Shell Programming : Conditional Statement
Aim
To write shell programs using decision-making constructs.
Shell supports decision-making using if statement. The if statement like its counterpart in programming languages has the following formats. The first construct executes the statements when the condition is true. The second construct adds an optional else to the first one that has different set of statements to be executed depending on whether the condition is true or false. The last one is an elif ladder, in which conditions are tested in sequence, but only one set of statements is executed.
if [ condition ]
|
if [ condition ]
|
if [condition ] |
The set of relational and logical operators used in conditional expression is given below. The numeric comparison in the shell is confined to integer values only.
Operator |
Description |
-eq |
Equal to |
-ne |
Not equal to |
-gt |
Greater than |
-ge |
Greater than or equal to |
-lt |
Less than |
-le |
Less than or equal to |
-a |
Logical AND |
-o |
Logical OR |
! |
Logical NOT |
Result
Thus using if statement scripts with conditional expressions were executed
Program 1. Odd or even
Algorithm
Step 1: Start
Step 2: Read number
Step 3: If number divisible by 2 then
Print "Number is Even"
Step 3.1: else
Print "Number is Odd"
Step 4: Stop
Program (oddeven.sh)
# Odd or even using if-else echo -n "Enter a non-zero number : " read num rem=`expr $num % 2` if [ $rem -eq 0 ] then echo "$num is Even" else echo "$num is Odd" fi
Output
[Administrator@localhost rathin] sh oddeven.sh
Enter a non-zero number : 12
12 is Even
Program 2. Biggest of 3 numbers
Algorithm
Step 1: Start
Step 2: Read values of a, b and c
Step 3: If a > b and a > c then
Print "A is the biggest"
Step 3.1: else if b > c then
Print "B is the biggest”
Step 3.2: else
Print "C is the biggest"
Step 4: Stop
Program (big3.sh)
# Biggest using logical expression
echo -n "Give value for A B and C: "
read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo "A is the Biggest number"
elif [ $b -gt $c ]
then
echo "B is the Biggest number"
else
echo "C is the Biggest number"
fi
Output
[Administrator@localhost rathin]$ sh big3.sh
Give value for A B and C: 4 3 4
C is the Biggest number
Program 3. Leap year
Algorithm
Step 1: Start
Step 2: Read the value of year
Step 3: If year divisible by 400 then
Print "Leap year"
Step 3.1: else if year divisible by 4 and not divisible by 100 then
Print "Leap year"
Step 3.2: else
Print "Not a leap year"
Step 4: Stop
Program (leap.sh)
# Leap year
echo -n "Enter a year : "
read year
rem1=`expr $year % 4`
rem2=`expr $year % 100`
rem3=`expr $year % 400`
if [ $rem3 -eq 0 ]
then
echo "$year is a Leap Year"
elif [ $rem2 -ne 0 -a $rem1 -eq 0 ]
then
echo "$year is a Leap Year"
else
echo "$year is Not a leap year"
fi
Output
[Administrator@localhost rathin]$ sh leap.sh
Enter a year: 1900
1900 is not a leap year
Program 4. Grade Determination
Algorithm
Step 1: Start
Step 2: Read mark
Step 3: If mark > 90 then
Print "S grade"
Step 3.1: else if mark > 80 then
Print "A grade"
Step 3.2: else if mark > 70 then
Print "B grade"
Step 3.3: else if mark > 60 then
Print "C grade"
Step 3.4: else if mark > 55 then
Print "D grade"
Step 3.5 : else if mark >= 50 then
Print "E grade"
Step 3.6: else
Print "U grade"
Step 4 : Stop
Program (grade.sh)
echo -n "Enter the mark : "
read mark
if [ $mark -gt 90 ]
then
echo "S Grade"
elif [ $mark -gt 80 ]
then
echo "A Grade"
elif [ $mark -gt 70 ]
then
echo "B Grade"
elif [ $mark -gt 60 ]
then
echo "C Grade"
elif [ $mark -gt 55 ]
then
echo "D Grade"
elif [ $mark -ge 50 ]
then
echo "E Grade"
else
echo "U Grade"
fi
Output
[Administrator@localhost rathin]$ sh grade.sh
Enter the mark : 65
C Grade
Program 5. String comparison
Algorithm
Step 1: Start
Step 2: Read strings str1 and str2
Step 3: If str1 = str2 then
Print "Strings are the same"
Step 3.1: else
Print "Strings are distinct"
Step 4: Stop
Program (strcomp.sh)
echo -n "Enter the first string : "
read s1
echo -n "Enter the second string : "
read s2
if [ $s1 == $s2 ]
then
echo "Strings are the same"
else
echo "Strings are distinct"
fi
Output
[Administrator@localhost rathin]$ sh strcomp.sh
Enter the first string : ece-a
Enter the second string : ECE-A
Strings are distinct
Program 6. Employee Pay Calculation
Algorithm
Step 1 : Start
Step 2 : Read basic
Step 3 : If basic > 30000 then hra is 5% of basic,da is 5% of basic and tax is 10% of basic
Step 3.1 : else if basic > 20000 then, hra is 4% of basic,da is 3% of basic and tax is 8% of basic
Step 3.2 : else hra is 3% of basic, da is 2% of basic and tax is 5% of basic
Step 4 : Stop
Program (emppay.sh)
echo -n "Enter employee basic pay : " read basic if [ $basic -gt 30000 ] then hra=`expr 5 \* $basic / 100` da=`expr 5 \* $basic / 100` tax=`expr 10 \* $basic / 100` elif [ $basic -gt 20000 ] then hra=`expr 4 \* $basic / 100` da=`expr 3 \* $basic / 100` tax=`expr 8 \* $basic / 100` else hra=`expr 3 \* $basic / 100` da=`expr 2 \* $basic / 100` tax=`expr 5 \* $basic / 100` fi gross=`expr $basic + $da + $hra` netpay=`expr $gross - $tax` echo "Gross Pay : $gross" echo "Net Pay : $netpay"
Output
[Administrator@localhost rathin]$ sh emppay.sh Enter employee basic pay: 12000 Gross Pay: 12600 Net Pay: 12000