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
Command line arguments
cat sample.sh echo Program:$0 The number of arguments is $# The arguments are $* grep$1 $2 echo \n Job Over $sample.sh hello test.txt
Command line arguments
Special variables are assigned to arguments in shell procedure First argument $1 Second argument $2
Arithmetic in shell scripts
Use to perform arithmetic operations. Syntax: expr op1 math-operator op2
Examples: $ expr 1 + 3 $ expr 2 - 1 $ expr 10 / 2 $ expr 20 % 3
Arithmetic in shell scripts
$ expr 10 \* 3 $ echo `expr 6 + 3`
Read and echo Commands
cat search.sh
#search for pattern in a file echo \n Enter the string to be searched:\c read pname echo \n Enter the file name:\c read fname echo \n Searching for $pname from file $fname \n grep $pname $fname echo \n Search completed\n
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
test command ( numeric)
Operato r
-eq -ne -gt -ge -lt -le
Meaning
Equal to Not equal to Greater than Greater than or equal to Less than Less than or equal to
test command ( numeric)
test : uses operator to evaluate the condition, returns true or false exit : used by if for taking decisions $x=5; y=7; z=7.2 $test 1 $test 0 $test 1 $test 0 $x eq $y ; echo $? $x lt $y ; echo $? $z gt $y ; echo $? $z eq $y ; echo $?
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
test command (file test)
Test
-e file -f file -r file -x file -d file -s file
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
-w file True if file exists and is writeable
test command (file test)
ls l sample.txt
-rw-rw-rw- 1 student SENG 870 Jun 8 15:52 sample.txt $[ -f sample.txt]; echo $?
0
$ [-x sample.txt]; echo $? 1 $ [! w sample.txt ] || echo File is writable File is writable
test command (file test)
$cat testfile.sh if [! e $1] ; then echo File does not exists elif [! r $1] ; then echo File is not readable elif [! w $1] ; then echo File is not writable else echo File is both readable and writable fi
The Case control structure
case expression in
pattern1) execute command;; pattern2) execute command;; pattern3) execute command;;
esac
The Case control structure
$ cat menu.sh
echo MENU\n 1.List of files\n2.Processes\n 3.Todays Date\n4.Users of system\n5.Quit \n Enter your choice :\c read choice case $choice in 1) ls l ;; 2) ps ;; 3) date ;; 4) who ;; 5) exit esac
The loop control structure
while until for loop Break continue
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
Directory stacks manipulation
dirs pushd popd
Job control processes
ps
list the processes running on the system
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.
$ hash hits command 1 /usr/bin/cat 2 /usr/bin/ps 4 /usr/bin/ls
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