Process commands depending on value of a variable.
When you need to handle more than three choices, switch
is a useful alternative to an if-then-else statement.
string variable matches
the first set of
commands are executed; if
pattern2, the second set of
commands are executed;
and so on. If no patterns match, execute commands under the
string can be specified using command substitution,
variable substitution, or filename expansion.
Patterns can be specified using pattern-matching symbols
]. breaksw is used to exit the switch after
are executed. If breaksw is omitted (which is rarely done),
the switch continues to execute another set of commands until
it reaches a breaksw or endsw.
Below is the general syntax of switch, side-by-side with an example
that processes the first command-line argument.
string) switch ($argv) case
pattern1: case -[
commandsnroff $file | lp breaksw breaksw case
pattern2: case -[
commandspr $file | lp breaksw breaksw case
pattern3: case -[
commandsmore $file breaksw breaksw . case -[
Ss]: . sort $file . breaksw default: default:
commandsecho "Error-no such option" exit 1 breaksw breaksw endsw endsw