Go to the previous, next section.

Conditional Constructs

if
The syntax of the if command is:

if test-commands; then
  consequent-commands;
[elif more-test-commands; then
  more-consequents;]
[else alternate-consequents;]
fi

Execute consequent-commands only if the final command in test-commands has an exit status of zero. Otherwise, each elif list is executed in turn, and if its exit status is zero, the corresponding more-consequents is executed and the command completes. If "else alternate-consequents" is present, and the final command in the final if or elif clause has a non-zero exit status, then execute alternate-consequents.

case
The syntax of the case command is:

case word in [pattern [| pattern]...) commands ;;]... esac

Selectively execute commands based upon word matching pattern. The `|' is used to separate multiple patterns.

Here is an example using case in a script that could be used to describe an interesting feature of an animal:

echo -n "Enter the name of an animal: "
read ANIMAL
echo -n "The $ANIMAL has "
case $ANIMAL in
  horse | dog | cat) echo -n "four";;
  man | kangaroo ) echo -n "two";;
  *) echo -n "an unknown number of";;
esac
echo "legs."

Go to the previous, next section.