Tutorials Point


  Unix for Beginners
  Unix Shell Programming
  Advanced Unix
  Unix Useful References
  Unix Useful Resources
  Selected Reading

© 2011 TutorialsPoint.COM


  Home     References     Discussion Forums     About TP  

Unix Shell - The if...elif...fi statement


previous next AddThis Social Bookmark Button


The if...elif...fi statement is the one level advance form of control statement that allows Shell to make correct decision out of several conditions.

Syntax:

if [ expression 1 ]
then
   Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
   Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
   Statement(s) to be executed if expression 3 is true
else
   Statement(s) to be executed if no expression is true
fi

There is nothing special about this code. It is just a series of if statements, where each if is part of the else clause of the previous statement. Here statement(s) are executed based on the true condition, if non of the condition is true then else block is executed.

Example:

#!/bin/sh

a=10
b=20

if [ $a == $b ]
then
   echo "a is equal to b"
elif [ $a -gt $b ]
then
   echo "a is greater than b"
elif [ $a -lt $b ]
then
   echo "a is less than b"
else
   echo "None of the condition met"
fi

This will produce following result:

a is less than b


previous next Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names