Control Instructions in Shell
There are four types of control instructions in shell-
1. Sequence Control Instructions
2. Selections and Decision Control Instruction
3. Repetition and loop control Instruction
4. Case Control Instruction
It is essential to have control over the order of execution of [Link] BASH offers 4
decision making instructions-
If-then-fi statement
If-then-else-fi statement
If-then-elif-else-fi statement
Case-Esac Statement
Simple if Example-
#ss13
echo enter source and target file names
read source target
If cp $source $target
then
echo File copied successfully
fi
Simple If-then-else-if example-
#ss14
echo enter source and target file names
read source target
If cp $source $target
then
echo File copied successfully
else
echo Failed to copy the file
fi
Exercise Question-
In a company an employee is paid as under-
If his basic salary is less than Rs.1500,then HRA=10% of basic salary, and DA=90% of basic
[Link] his salary is either equal to or above Rs 1500, then HRA=Rs 500 and Da=98%of Basic
[Link] the employers salary is input through the keyboard ,Write a program to find his gross .
salary.
Operators-(When we want to compare numbers)
-gt greater than
-ltless than
-gegreater than equal to
-leless than equal to
-nenot equal to
-eqequal to
File Tests-
We have a few operators that can be used to test various properties associated with a Unix [Link]
a variable file holds an existing file name "test" the size of which is 100 bytes and has read, write and
execute permission on –
-e file exists
-a file exists This is identical in effect to -e. It has been "deprecated," [1] and its use is discouraged.
-f file is a regular file (not a directory or device file)
-s file is not zero size
-dfile is a directory
-bfile is a block device
-cfile is a character device
-rif file exists and have read permission on it.
-w if file exists and have write permission on it.
-x if file exists and have execute permission on it.
-kIf file exists and sticky bit is set on it.
File Test in shell script
Q--Shell Script:- To check whether a file exist or not
echo Enter Filename
Read fname
If [ -f $fname ]
Then
Echo file exists
Else
Echo file does not exist
Fi
Q2—Shell script- To check whether the user has write permission to the [Link] Yes then append
something into the file.
echo Enter Filename
Read fname
If [ -w $fname ]
Then
Echo file exists Happy Unix > [Link]
Else
Echo No write permisiion
Fi
String Test
Here is the program to put these string test into action:-
str1="Good"
str2="Bad"
str3=
[$str1 = $str2 ]
echo $?
[$str1 != $str2 ]
echo $?
[ -n $str1 ]
echo $?
[-z "$str3" ]
echo $?
[-z $str3 ]
echo $?
["$str3"]
echo $?
Shell Script- Predict the output of the program?
Str1=”Good Morning”
Str2=”Good Bye”
[“$str1”=”$str2”]
Echo $?
(Since str1 and str2 are having two words , so we need to enclose them in double quotes)
Use of Logical Operators
1. AND(-a)
2. OR(-o)
3. NOT(!)
Shell script regarding the same-
echo "enter marks in five subjects \c"
read m1 m2 m3 m4 m5
add=`expr $m1 + $m2 + $m3 + $m4 + $m5`
per=`expr $add / 5`
echo percentage is $per
if [ $per -ge 60 ]
then
echo First Division
fi
if [ $per -ge 50 -a $per -le 60 ]
then
echo second division
fi
if [ $per -ge 40 -a $per -lt 50 ]
then
echo Third Division
fi
if [ $per -lt 40 ]
then
echo Fail
fi
Shell Script Examples-
echo "Enter filename"
read fname
if [ -w $fname -a -r $fname -a -x $fname ]
then
echo File has all permission !!! Happy Unix !!!
else
echo File has no permission
fi
Use of Nested else if with the elif clause
echo "Enter the adapter Name"
read adapter
if [ "$adapter" = "MA" ]
then
echo monochrome adapter
elif [ "$adapter" = "CGA" ]
then
echo colur graphic adapter
elif [ "$adapter" = "EGA" ]
then
echo enhanced graphic adapter
elif [ "$adapter" = "VGA" ]
then
echo Video graphic adapter
else
echo super video graphic adapter
fi
Case Control Structure
Another way of controlling the sequence of execution is using the case control structure.
Syntax with Example—
echo "Enter the number from 1 to 3:"
read num
case $num in
1)echo You entered 1 ;;
2)echo you entered 2 ;;
3) echo you entered 3 ;;
*)echo i said 1 to 3! ;;
esac
Tips and tricks -:
1. You can put the cases in any order you like.
2. The value portion of case statement can be a shell variable ,a shell script argument or output of
a command.
For example- case `who am I |cut –f1 ` in
Or
Case $1 in
3. If we want two choices options we can combine the options using the or (|) operator.
For example-
Case $1 in
Cat|dog)echo you supplied the name of animals ;;
Parrot|crow) echo You supplied the name of bird ;;
Whale|shark)echo You supplied the name of fishes ;;
*) echo you supplied an incorrect argument ;;
esac
4.