Wednesday, September 5, 2012

Basic of Shell Script


What Is a Shell?


Ø       The shell is a user program or it is an environment provided for user interaction.
Ø       It is a command language interpreter that executes commands read from the standard input device such as keyboard or from a file.
Ø       The shell gets started when you log in or open a console (terminal).
Ø       Quick and dirty way to execute utilities.
Ø       The shell is not part of system kernel, but uses the system kernel to execute programs, create files etc.
Ø       Several shells are available for Linux including:
Ø       BASH ( Bourne-Again SHell ) - Most common shell in Linux. It's Open Source.
Ø       CSH (C SHell) - The C shell's syntax and usage are very similar to the C programming language.
Ø       KSH (Korn SHell) - Created by David Korn at AT & T Bell Labs. The Korn Shell also was the base for the POSIX Shell standard specifications.
Ø       TCSH - It is an enhanced but completely compatible version of the Berkeley UNIX C shell (CSH).
Please note that each shell does the same job, but each understands different command syntax and provides different built-in functions. Under MS-DOS, the shell name is COMMAND.COM which is also used for the same purpose, but it is by far not as powerful as our Linux Shells are!


Shell Prompt

There are various ways to get shell access:
Ø       Terminal - Linux desktop provide a GUI based login system. Once logged in you can gain access to a shell by running X Terminal (XTerm), Gnome Terminal (GTerm), or KDE Terminal (KTerm) application.
Ø       Connect via secure shell (SSH) - You will get a shell prompt as soon as you log in into remote server or workstation.
Ø       Use the console - A few Linux system also provides a text-based login system. Generally you get a shell prompt as soon as you log in to the system.

How do I find Out My Current Shell Name?


To find all of the available shells in your system, type the following command:
cat /etc/shells
In case the shells file has more than one shell listed under it, then it means that more than one shell is supported by your Platform

cat /etc/shells

Bash and Command Types


The bash shell understands the following types of commands:
Ø       Aliases such as ll
Ø       Keywords such as if
Ø       Functions (user defined functions such as genpasswd)
Ø       Built in such as pwd
Ø       Files such as /bin/date
The type command can be used to find out a command type.

type command

The type command can be used to find out if a command is built in or an external binary file.

Find out if ls is built in or an external command

Type the following command at a shell prompt:
type -a  ls
Sample Output:

ls is /bin/ls

To find out if history command is built in or an external command, enter:
type -a  history
 
sample Output:

history is a shell built in

However, some commands are supplied as both internal and external commands. For example:
 
type -a true
type -a echo
sample Outputs:

echo is a shell built in
echo is /bin/echo


Shebang

The #! syntax used in scripts to indicate an interpreter for execution under UNIX / Linux operating systems. Most Linux shell and perl / python script starts with the following line:
#!/bin/bash
OR
#!/usr/bin/perl
OR
#!/usr/bin/python
OR lisp script : 
 #!/usr/local/bin/sbcl --script
     (write-line "Hello, World!")
 

Starting a Script With #!


1.     It is called a shebang or a "bang" line.
2.     It is nothing but the absolute path to the Bash interpreter.
3.     It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash.
4.     All scripts under Linux execute using the interpreter specified on a first line.
5.     Almost all bash scripts often begin with #!/bin/bash (assuming that Bash has been installed in /bin)
6.     This ensures that Bash will be used to interpret the script, even if it is executed under another shell.
7.     The shebang was introduced by Dennis Ritchie between Version 7 Unix and 8 at Bell Laboratories. It was then also added to the BSD line at Berkeley.

Ignoring An Interpreter Line (shebang)

Ø       If you do not specify an interpreter line, the default is usually the /bin/sh. But, it is recommended that you set #!/bin/bash line.

Setting up permissions on a script


The chmod command (change mode) is a shell command in Linux. It can change file system modes of files and directories. The modes include permissions and special modes. Each shell script must have the execute permission. Mode can be either a symbolic representation of changes to make, or an octal number representing the bit pattern for the new mode bits.

Examples

Allowing everyone to execute the script, enter:
chmod +x script.sh
OR
chmod 0755 script.sh
Only allow owner to execute the script, enter:
chmod 0700 script.sh
OR
chmod u=rwx,go= script.sh
OR
chmod u+x script.sh
To view the permissions, use:
ls -l script.sh
Set the permissions for the user and the group to read and execute only (no write permission), enter:
chmod ug=rx script.sh
Remove read and execute permission for the group and user, enter:
chmod ug= script.sh

Execute a script


A shell script can be executed using the following syntax:
chmod +x script.sh
./script.sh
You can also run the script directly as follows without setting the script execute permission:
bash script.sh
. script.sh
In last example, you are using. (dot) command (a.k.a., source) which reads and executes commands from filename in the current shell. If filename does not contain a slash, directory names in PATH are used to find the directory containing filename.
When a script is executed using either the bash command or the dot (.) command, you do not have to set executable permissions on script but read permissions need to be set.

0 comments:

Powered by Blogger.