Shell Control Structures
Till now we have deal with sequential control instructions. In which the instruction are executed in a sequential order. But some times we have to choose the choice, like such happen when that is true and if not then this happen. This can be done by using the Decision control instruction. Like if
– else, case control structure (switch statement in C), String comparisons and many more.
Let us discuss each of them:
Use of if – else expression:
For using the if – else expression, the shell provide us a special command called “test”. The “test”
command can perform following types of tests:
1. Numerical tests
2. String tests
Numerical Tests:
The “test” command handles various numerical comparisons on integers, such as a number is greater than, less then and equal to and many more. The shell provides following set of operators:
(1) –eq equal to
(2) –ne not equal to
(3) –gt greater than
(4) –lt less than
(5) –ge greater than or equal to
(6) –le less than or equal to
Let us under the use of these operators by writing a shell script to find out that the both numbers are equal or not.
$cat > punit
# find both number are equal or not
echo Enter the two numbers
read a b
if test $a –eq $b
then
echo The two numbers are equal
else
echo The two numbers are not equal
fi
[Ctrl + d]
$_
When we execute the program we get:
Enter the two numbers
10 10
The two numbers are equal
OR
Enter the two numbers
10 11
The two numbers are not equal, there is an alternative method of “test” command is square brackets [ ]. Suppose we want to find the greatest number among the three.
$cat > punit
# find greatest number among the three
echo Enter three numbers
read a b c
big = $a
if [ $big –lt $b ]
then
$big = $b
fi
if [$big – lt $c ]
then
$big = $c
fi
echo Largest number is $big
[Ctrl + d]
$_
When we execute this script, we will get:
Enter three numbers
10 1 12
Largest number is c.
From this script it is clear that the command [ $big –lt $b ] is the same as “ test $big –lt $b” while using the square bracket in place of a test command. Also note that a blank space should be placed immediately after “[“ and immediately before “]”, otherwise it would be treated as an invalid expression.
String Tests:
A string is just a series of characters. It may be a filename, a command name, or the value of a variable. The shell provides following set of options to handle string tests:
(1) string1 = string2 true, if the strings are equal
(2) string1! = string2 true, if the strings are not equal
(3) –n string true, if the strings has non – zero value
(4) –z string true, if the strings has zero value
Let us see a shell script program in which we enter two strings and check that whether two strings are equal or not:
$cat > stringcheck
#comparison of two string
echo Enter two strings
read first second
if [ $first = $second ]
then
echo The two strings are equal
else
echo The two strings are not equal
fi
[Ctrl + d]
$_
When we execute that shell script we get:
Enter two strings
Punit Punit
The two strings are equal
OR
Enter two strings
Punit punit
The two strings are not equal Logical operators:
The shell provide us three types of logical operator while performing a test as:
(1) –a binary AND operator
(2) –o binary OR operator
(3) ! binary NOT operator
Let us understand these operators by using a shell script in which we have find out which number is greater among the three:
$cat > punit
# find greatest number among the three
echo Enter three numbers
read a b c
if [ $a –gt $b –a $a –gt $c ]
then
echo The greatest number is $a
elif [$b –gt $a –a $b –gt $c ]
then
echo The greatest number is $b
else
echo Largest number is $c
[Ctrl + d]
$_
When we execute the following shell script we get the following output:
Enter three numbers
10 1 12
The largest number is c
The Case Statements:
The case statement is used to select one option form a list of alternatives. In case statements we use two keywords “case” and “esac” (case spelt backward). The choice in case statements is represented as “choice1)” ,”choice2)”; here you can see that only right parenthesis is used. The right parenthesis ) is used to identify label name. *) is used for default case. Remember that double semicolon (;;) is encountered immediately when our choice is completed. The double semicolon transferred the control to the keyword “esac” that makes the end of the case statements. Actually double semicolon is not necessary after the final choice, but there is no harm to use it there.
Let us write down a simple program of case statement:
$cat > punit
#case statements
echo Enter any day number from 1 to 7
read no
case #no in
1) echo Monday;;
2) echo Tuesday;;
3) echo Wednesday;;
4) echo Thursday;;
5) echo Friday;;
6) echo Saturday;;
7) echo Sunday;;
*) The entered choice is wrong;;
esac
[Ctrl + d]
$_
After executing the shell script we get the following output
Enter any day number from 1 to 7
1
Monday
Enter any day number from 1 to 7
0
The entered choice is wrong
In the last example of case statement we have enter numbers to the value portion of the case statement and label cases. We can also use the words at “case choice” places: for example
$cat > punit
echo who
echo pwd
echo ls
echo date
echo Please enter the choice
read choice
case $choice in
who) who;;
pwd) pwd;;
ls) ls;;
date) date;;
*) You enter the wrong choice
esac
[Ctrl + d ]
$_
Output is something like that
who
pwd
ls
date
Please enter the choice
pwd
/usr/punit
And so on……