case - Unix, Linux Command



NAME

case - To conditionally perform a command, case will selectively execute the command-list corresponding to the first pattern that matches word.

SYNOPSIS

case word in [ [(] pattern [| pattern]...) command-list ;;]... esac

DESCRIPTION

The '|' is used to separate multiple patterns, and the ')' operator terminates a pattern list. A list of patterns and an associated command-list is known as a clause. Each clause must be terminated with ';;'. The word undergoes tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal before matching is attempted. Each pattern undergoes tilde expansion, parameter expansion, command substitution, and arithmetic expansion. There can be an arbitrary number of case clauses, each terminated by a ';;'. The first pattern that matches determines the command-list that is executed.

EXAMPLES

Create a Utility Class to use case.

#casesample
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."

Run the utility

$ chmod a+x casesample
$ ./casesample
Enter the name of an animal: dog
The dog has four legs.
Print
Advertisements