Sunday, August 15, 2010
10:28 AM

Conditional Statements

Using conditional statements in scripting.

Often in scripts we might want to certain things only if some other condition is satisfied
For e.g.:
Display the contents of a file if the file exists.

For such conditions we use the "if" statement in scripting.

Syntax


if("condition statement") then


{block of commands to be executed } 


fi


The "if" statement starts by checking some condition mentioned in the "condition statement" . If this condition is true, then it executes the block of statements given next until the "fi". Which marks the end of the if block.
After the "fi" the script continues as usual.

If the condition turns out to be false, then the script directly jumps to the statement after "fi", not executing any of the statements between "if" and "fi".

The conditions are checked using the "test" command. The test command has various options available with it, which are listed in the man page of test.

for e.g.:
To check the existence of a file  we use  " test -e "file name"  "
So with the "if" statement if would be

#!/bin/bash 
if( test -e temp) then
echo "It exists " 
fi

Save the file as if.sh, give execute permission and run it.

If your current working directory has a file by the name "temp", then the output will be
"It exists"  other wise there will be no output.

If you want to compare two numbers

#!/bin/bash


echo "Enter the first number" 
read i;
echo "Enter another number" 
read j;
if( test $i -gt $j) then
   echo "$i is greater"
fi
if( test $j -ge $i) then
        echo "$j is greater or equal"
fi

Save the script as if2.sh, give execute permission and run it.

The script will prompt you to enter 2 numbers, The numbers you enter are compared
with each other using the test command. Note that to compare numbers we use the letter

-gt greater than
-lt lesser than
-eq equal to
-ge greater than or equal to
-le lesser than or equal to
-ne not equal to.

Note that these letters are used only with numbers and not with strings or characters. String comparison is shown below.

The stings are compared using

STRING1 = STRING2
STRING1 != STRING2
-n STRING To check if the string length is non Zero
-z String to check if the string length is Zero.

Eg:

#!/bin/bash 
password="pass" 
read  auth
if (test $auth = $pass)then 
echo "Log in successful" 
fi

Save the above script as string_chk.sh, give it execute permission and run it.

The script has a variable, password, that has the value "pass". The script prompts the user to enter a string. The string entered by the user is checked with the previously stored string, if they match then script enters the "if" block and prints "Log in successful".

In the example above we printed "Log in successful" when the user enters the correct string, but what about the condition when the user does not enter the correct sting.
For such cases we need to make use of the "else" statement.

Eg:

#!/bin/bash 
password="pass" 
read  auth
if (test $auth = $pass)then 
echo "Log in successful" 


else 
echo "Wrong password" 


fi

Save the above script as else.sh, give it execute permission and run it.

The script should print "Log in successful" if you enter the correct string, and print
"Wrong password" if you enter the wrong string.
The "else" block is executed only when the "if" condition fails or is false.

Now one more thing that we might want to do is check multiple conditions.

for eg:

#!/bin/bash 
echo "enter your age" 
read age


if(test $age -gt 18) then
echo "you are eligible for license" 


elif(test $age -gt 16)then 
echo "you can get a temporary license" 


else 
echo "you will have to wait till you become 18" 


fi

Save the script as elseif.sh, give it execute permission and run it.

The script prompts the user to enter their age, the age is compared with the number "18". If it is greater than 18, then it prints out the message "You are eligible for license"
. If this comparison turns out false, that is the age entered is less than 18, then it is the age is again compared with "16". To this second comparison, we use "elif" .
"elif" is executed only if the condition in the previous "if" condition turns out to be false. We can have as many "elif" one after the other as we want, each "elif" would be executed only if all the "if" and "elif" above it turn out to be false.
 If the condition in the "elif" also turns to be false we have included as final else, which is executed when all the "if" and "elif" become false.

Bash also has a special feature, instead of using the test condition we can use "[".
They both work in the same way.
For eg:

#!/bin/bash 
echo "enter your age" 
read age


if [ $age -gt 18] 
then
echo "you are eligible for license" 


elif [ $age -gt 16 ]
then 
echo "you can get a temporary license" 


else 
echo "you will have to wait till you become 18" 


fi

Save the above script as "bracket.sh", give it execute permission and run it.

The script should run exactly same as the previous example elseif.sh.
But in this script, if you notice, we have not used the "test" statement but instead we have used the "[" which is same are "test" in bash. The only difference between the usage of "test" and "[" are
The "[" has the followed by a empty space, as it is also a command. But in case of test we need not have a space after the "(".
The "then" statement comes in the next line.
The space before the closing "]" is also required.

Bash also supports nested ifs, i.e we can use a if condition when inside another if block.
For eg:

#!/bin/bash 
echo "enter the car name"
read car 
echo "Enter the year of manufacture"
read year 


if [ $car = "mercedes" ]
then 
  if [ $year -gt 2000 ]
  then
echo "I will buy" 
   else 
echo "its too old"
   fi 
else 
echo "Not my type of car"
fi 

Try it out:

1. Write a script that will ask the user year of birth, then calculate his age and
   and then print out if he is an adult or a minor.
2. Write a script that will ask the user to enter their marks and output their grade     based on the following scale
90-100 Grade A
70-90  Grade B
60-70  Grade C
50-60  Grade D
< 50   Grade E

0 comments:

Post a Comment