next up previous contents
Next: The case command Up: Control Previous: Logical expressions   Contents

The if command

Use if for simple branching for example:-

echo -n "Try to guess the answer "
read ans
if [ $ans -lt 42 ]
then
  echo "No, too low"
elif [ $ans -gt 42 ]
then
  echo "No, too high"
else
  echo "Ah, the ultimate answer!"
fi

The elif and else sections are optional. Hint: make sure you leave spaces between all tokens e.g. don't code e.g.:-

if [$ans -lt 42]



P.D. Gronbech (IT Staff) 2015-10-02