shell中case语法如下:
case word in
pattern1)
Statement(s) to be execute if pattern1 matchs
;;
pattern2)
Statement(s) to be execute if pattern2 matchs
;;
pattern3)
Statement(s) to be execute if pattern3 matchs
;;
*)
Default action
;;
esac
【注】pattern模式不能包含元字符:*、?、[..](类,如[a-z]等)
【注】pattern模式里面可以包含或符号(|),表示多个匹配,如y|Y|yes|YES。
例子:
#!bin/sh
# by lsj@baidu.com
if [ $# -ne 3 ]
then
echo "caculator followed 3 para"
exit 1
fi
op_a=$1
op=$2
op_b=$3
case $op in
+|-|*|/)
result=`echo "$op_a $op $op_b" | bc`
echo $op_a$op$op_b=$result
;;
*)
echo "wrong operator"
esac