Wednesday, February 16, 2011

Shell

The shell allows you to chain commands together into a single step. You can use semicolon and put both commands on the same line, but in a shell script, you can list commands on separate lines. The shell will process commands in the order in which they appear in the file.

Creating a Script File

1. use a text editor to create a file, then enter the command to the file

2. specify the shell you are using, format: #!/bin/bash

3. give permission to execute the file: $chmod u+x test

4. $./test

Structured Command:
1. if -then
$ cat test1
#!/bin/bash
# testing the if statement
if date
then
echo "it worked"
fi

$ ./test1
Thu Feb 24 15:29:12 EST 2011
it worked

Shell is a user program or it's environment provided for user interaction. Shell is an command language interpreter that executes commands read from the standard input device (keyboard) or from a file.

Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc.

Several shell available with Linux including: BASH, CSH, KSH, TCSH

Tip: To find all available shells in your system type following command:
$ cat /etc/shells

Tip: To find your current shell type following command
$ echo $SHELL

Normally shells are interactive. It means shell accept command from you (via keyboard) and execute them. But if you use command one by one (sequence of 'n' number of commands) , then you can store this sequence of command to text file and tell the shell to execute this text file instead of entering the commands. This is know as shell script.

Following steps are required to write shell script:

(1) Use any editor like vi or mcedit to write shell script.

(2) After writing shell script set execute permission for your script as follows
syntax:
chmod permission your-script-name

Examples:
$ chmod +x your-script-name
$ chmod 755 your-script-name

Note: This will set read write execute(7) permission for owner, for group and other permission is read and execute only(5).

(3) Execute your script as
syntax:
bash your-script-name
sh your-script-name
./your-script-name

Examples:
$ bash bar
$ sh bar
$ ./bar

NOTE In the last syntax ./ means current directory, But only . (dot) means execute given command file in current shell without starting the new copy of shell, The syntax for . (dot) command is as follows
Syntax:
. command-name


Debug a script:

$bash -x script_name

$bash -xv script_name

cd to /etc/init.d and view various system init scripts:

cd /etc/init.d
ls
vi ssh

Display your current PATH:

echo $PATH

Sample outputs:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Customize your PATH variable and remove /usr/games from PATH:

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH=$PATH:/usr/games

The whatis command is used display a short description about command. whatis command searches the manual page names and displays the manual page descriptions for a command:

whatis command-name
whatis date
whatis ifconfig
whatis ping

Change password:
$passwd

Create alias:
alias name='command'
alias name='command arg1 arg2'

Remove alias:
unalias alias-name

Go to root: $su

alias update='apt-get update && apt-get upgrade'

Doing logout and login back operation: $bash