之前提到的“if...then...fi”是通过比较来判断变量的,如果符合状态就进行某些行为动作,并且通过较多层次(就是elif...)的方式,进行多个变量的判断,其也可以使用case...in...esac方式进行实现
1.case...in...esac格式
case $变量名称 in
"变量内容1")
程序段
;;
"变量内容2")
程序段
;;
*)
程序段
;;
esac
2.样例
[root@kibana ~]# cat case.sh
#!/bin/bashread -p "Please input(Y/N): " yncase $yn in"Y" | "y")echo "Yes,continue";;"N" | "n")echo "No,interrupt";;*)echo "I don't know what you choice is!";;
esac
[root@kibana ~]# sh case.sh
Please input(Y/N): y
Yes,continue
[root@kibana ~]# sh case.sh
Please input(Y/N): n
No,interrupt
[root@kibana ~]# sh case.sh
Please input(Y/N): Y
Yes,continue
[root@kibana ~]# sh case.sh
Please input(Y/N): N
No,interrupt
[root@kibana ~]# sh case.sh
Please input(Y/N): dddd
I don't know what you choice is!
[root@kibana ~]#