Linux Shell Programming
Linux Shell Programming
Overview
Command line arguments Arithmetic in shell scripts Read and echo Commands in shell scripts Taking decision:
If then-fi If then else-fi The test command(file test,string tests) Nested if-elses The Case control structure
Overview
The loop control structure
The while ,until and for loop structure The break and continue statements
Directory stacks manipulation Job control, history and processes Built in functions
Taking decision
If then-fi
if condition is true then execute commands fi
If then else-fi
if condition is true then execute commands else execute commands fi
If then else-fi
if grep test sample.txt then echo String found else echo String not found fi
Meaning
Equal to Not equal to Greater than Greater than or equal to Less than Less than or equal to
test command
if test $# -ne 3 then echo 3 arguments are required exit 3 else
if grep $1 $2 >$3 then echo String found else echo String not found fi
fi
Exit status
True if file exists True if file exists and is a regular file True if file exists and is readable True if file exists and is executable True if file exists and is directory True if file exists and has size > 0
0
$ [-x sample.txt]; echo $? 1 $ [! w sample.txt ] || echo File is writable File is writable
esac
The while ,until and for loop structure while condition is true do execute commands done
The while ,until and for loop structure for variable in list do execute commands done
kill
send a signal to one or more processes (usually to "kill" a process)
jobs
listing processes
bg
put a process in the background
fg
put a process in the foreground
history
To display the command list
Built in functions
hash Command
hash command maintains a hash table When a command is executed it searches for a command in variable $PATH.
Built in functions
$ hash -d cat $ hash hits command 2 /usr/bin/ps 4 /usr/bin/ls
Built in functions
set is a shell built-in command: - used to set and modify the internal
variables
Built in functions
$ cat set.sh var="Welcome to CDAC" set -- $var echo "\$1=" $1 echo "\$2=" $2 echo "\$3=" $3 $ ./set.sh $1=Welcome $2=to $3=CDAC
unset
- set the shell variable to null $ cat unset.sh var="welcome to CDAC" echo $var unset var echo $var $ ./unset.sh welcome to CDAC
let
Performs arithmetic operations on shell variables $ cat arith.sh
let arg1=12 let arg2=11 let add=$arg1+$arg2 let sub=$arg1-$arg2 let mul=$arg1*$arg2 let div=$arg1/$arg2 echo $add $sub $mul $div $ ./arith.sh 23 1 132 1
Reference
1.http://www.thegeekstuff.com/2010/08/bas h-shell-builtin-commands/ 2.UNIX Concepts And Application Sumitabha Das 3.http://linuxcommand.org/lts0080.php