Friday, July 26, 2013

SHELL CUSTOMIZATION



SETTING ENVIRONMENT (BASH)

.bash_profile:  This file contains the commands that define the basic environment for the user
                        login account. This file is read and executed each time the user logs in.

.bash_logout:    This file is read and executed every time a login shell exits.

.bashrc:            If we start up a new shell (sub shell) by typing bash on the command line, it attempts to read commands from the file .bashrc. This allows the flexibility of separating the startup commands needed at the login time from those we might need when we run a sub shell.

Debugging a shell program:

set –n               do not run commands; check for syntax errors only

set –v               echo commands before running them

set –x               echo commands after command-line processing

Processes and Jobs:

Unix gives all processes numbers called PIDs. A job number is assigned by the shell not by the operating system. Job nos. refer to the background processes that are currently running under your shell, whereas pids refer to all processes running on all computers. A job basically refers to a command line that was invoked from the shell.

$ ps                              list all processes on the current terminal
$ ps –a                         lists all processes on all terminals

$ jobs                           to list the current list of jobs

$fg                               to bring a background job to foreground

$bg                              to bring a foreground job to background


$ kill %1                        to kill a job with job no. 1
$ kill –KILL  %1 sure kill for the job
$ kill -9 PID                   sure kill for the process

Signals:
A signal is a message that one process sends to another when some abnormal even takes place or when it wants the other process to do something.
Ex: control C

Trap:
We can trap specific signals and process them in their own way.
trap command sig1 sig2

ex:

trap “echo you have hit control C or control Z ’ INT TERM
while true
do
            sleep 60
done

if we give null string as the command argument to trap, then the shell will ignore that signal

Export:
By default, the values stored in shell variables are local to the shell and are not passed on to a child shell. However, with export command these variables can be made global.

Exec:
Overwrites the code of the program with the code of new program.

Eval:
Eval evaluates twice or parses twice

I/O Redirectors:
>, <, >>

Make:
$ vi makefile
main:    sub1.o sub2.o sub3.o
            cc –o main.c sub1.o sub2.o sub3.o –lm

sub1.o: sub1.c
            cc –c sub1.c
sub2.o : sub2.c
            cc –c sub2.c
sub3.o: sub3.c
            cc –c sub3.c

Ar:
Archives the files
-r          adds a file
-q         appends a file at the end of the archive

-x         extracts a file from the archive

-d         deletes a file from the archive

-t          gives table of contents of the archive

-v         verbose option

$ ar –rv lib.a lib1.o lib2.o


SHELL CONSTRUCTS



Shell has the facilities for writing programs. It has the programming constructs to perform logical operations.

Conditions on numbers:
-gt        greater than
-ge       greater than or equal to
-lt         less than
-le         less than or equal to
-eq       equal to
-ne        not equal to

conditions on strings
>          greater than
<          less than
=          equal to
!=         not equal to
-z          zero length
-n         non-zero length

conditions on files:
-r          readable file
-w         writable file
-x         executable file
-s         file exists and not empty
-f          ordinary file
-d         directory
-l          symbolic link


1.if

if          [ condition ]
then
            statements
fi


if          [ condition ]
then
            statements
else
            statements
fi

if          [ condition ]
then
            statements
elif        [ condition ]
then
            statements

fi

2. while:

while     [ condition ]
do
            statements
done

3. until

until      [ condition ]
do
            statements
done

4. for

for variable in list
do
            statements
done

5. arithmetic for:

for (svalue; evalue; incr/decree)
do
            statements
done




6. case

case “value” in

pattern 1 )         statements
                        break;;

patter 2 )           statements
                        break;;


*           )           statements
                        break;;

esac


7. select

select name in list
do
            statements
done


Arrays:

We can arrays using shell. The subscripting will start from 0 to six hundred billion.

Ex:

K=1
While [ $k –lt 10 ]
do
            read val[$k]
            let k++
done

k=1
while [ $k –lt 10 ]
do
            printf “{val[$k]}\n”}
            let k++
done




functions:

function abc {
                        shell commands
                        return (optional)
                        }

A local statement inside a function definition makes the variables involved all become local to that function.

Function abc {
                        local a
                        shell commands
                        }


INTERNAL AND EXTERNAL COMMANDS



The Shell has a number of built-in commands. These are called internal commands. Ex: cd, echo etc. These do not generate a process and are executed directly by the Shell.

The commands that are outside the Shell architecture are known as external commands. Ex: cat, ls etc. The Shell creates a process for each of these commands.

type command will let us know whether a command is internal or external.

echo and printf :
These commands are used to print messages on the screen.
$ echo computer
$ printf “computer \n”

Shell Variables And Their Assignment

$ var=value
$ x=123
$ y=”data”
if x is a variable, $x indicates its value.

We can input the values into the variables using read.

$ read x y

using declare command, we can set controls over the input.

$ declare –r a ( read only)
$ declare –i b ( integer only)

let command allows to perform various arithmetic expressions,

$ x=1
$ y=2
$ let z =x+y  ( +, -, *, /, %)
printf “$z\n”

expr command is used perform string related operations

$ a=”data”
$ expr length $a ( finds the length of the variabl)

$ expr substr $a  1 2 ( finds the substring from 1 to 2 position nos. )

$ expr index $a “t”  (finds the position no. of the character in the string)

alias is used to create own command in our environment.

$ alias l=’ls –l’
$ l (will give the result of ls –l)