case str in
pat1) command(s);;
pat2) command(s);;
pat3) command(s);;
esac
case string in
pat1) command(s) ;; #若 $string = pat1, 則執行 command(s)
pat2|pat3) command(s) ;; #若 $string = pat1 或 pat2, 則執行 command(s)
*) command(s) ;; #default case
esac
其中供比對的 pattern 可以是以下三種形式中的任一種字串:
使用者給的固定字串;
執行指令得到的結果 (Command Substitution)
含有萬用字元( wildcards 或 meta characters)的字串如下:
*
任意字串,包括 null
?
任意字元
[abc]
a, b, 或c三字元其中之一
[a-n]
從a到n的任一字元
[!abc]
a, b, 或c以外的字元
|
多重選擇
以下是兩個例子:
case $# in
0) echo Usage: xxxxxx ;;
1|2) process data ;;
*) echo Usage: xxxxx ;;
esac
echo 'Enter A, B, or C: \c'
read letter
case $letter in
A|a) echo 'You entered A.';;
B|b) echo 'You entered B.';;
C|c) echo 'You entered C.';;
*) echo 'Not A, B, or C';;
esac