Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1
esac

You can specify a number of discrete values—such as str1, str2, and so
on—for each condition, or you can specify a value with a wildcard. The last
condition should be an asterisk (*) and is executed if none of the other
conditions are met. For each of the specified conditions, all the associated
statements until the double semicolon (;;) are executed.


You can write a script that echoes the name of the month if you provide the
month number as a parameter. If you provide a number that isn’t between 1
and 12, you get an error message. The script is as follows:


Click here to view code image
#!/bin/sh


case    $1  in
01 | 1) echo "Month is January";;
02 | 2) echo "Month is February";;
03 | 3) echo "Month is March";;
04 | 4) echo "Month is April";;
05 | 5) echo "Month is May";;
06 | 6) echo "Month is June";;
07 | 7) echo "Month is July";;
08 | 8) echo "Month is August";;
09 | 9) echo "Month is September";;
10) echo "Month is October";;
11) echo "Month is November";;
12) echo "Month is December";;
*) echo "Invalid parameter";;
esac

You need to end the statements under each condition with a double semicolon
(;;). If you do not, the statements under the next condition are also executed.


The format for a case statement for tcsh is as follows:


Click here to view code image
switch (str)
case str1|str2:
Statements
breaksw
case str3|str4:
Statements
breaksw
default:
Statements
breaksw
endsw

Free download pdf