学过其他语言的应该也知道case怎么用,用来干嘛。这里就讲shell的好了
case语句会采用列表格式来检查单个变量的多个值,格式如下
case var in
pattern1|pattern2) commands1;;
pattern2) command2;;
*) default commands;;
esac
实际例子如下:
#!/bin/bash
#using the case command
#
read -p "请输入一个数字:" num
case $num in
1|2)
echo "this is one and two";;
3)
echo "this is three";;
4)
echo "this is four";;
*)
echo "this is other num";;
esac
运行结果如下: