Shell multi-branch case statement

a grammar
The case syntax is a multi-branch conditional statement like the if...elif...else statement, but unlike the if multi-branch conditional statement, the case statement can only judge one conditional relationship, while the if statement can judge multiple conditions relation.
case $variable name in
"值1")
If the value of the variable is equal to 1, execute program 1
;;
"值2")
If the value of the variable is equal to 1, execute program 2
;;
...omit the other branches..
*)
If the values ​​of the variables are not the above values, execute this procedure
;;
esac
 
Two actual combat
#!/bin/bash
read -p "please choose yes/no:" -t 30 cho
case $cho in
"yes")
echo "Your choose is yes"
;;
"no")
echo "Your choose is no"
;;
*)
echo "Your choose is error"
;;
esac
 
Three tests
[root@localhost shell]# ./shell7.sh
please choose yes/no:yes
Your choose is yes
[root@localhost shell]# ./shell7.sh
please choose yes/no:no
Your choose is no
[root@localhost shell]# ./shell7.sh
please choose yes/no:fd
Your choose is error
 
Four actual combat 2
#!/bin/bash
echo "fengj : qing shuru 1"
echo "fur:qing shuru 2"
echo "buod:qing shuru 3"
 
read -t 30 -p "qing shuru xuanze" cho
case "$cho" in
"1")
echo "fengj gengni"
;;
"2")
echo "fur jiagei ni"
;;
"3")
echo "fuduo he wo zou"
;;
*)
echo "qing shuru zhengq xuanze"
;;
esac
 
Five Test 2
[root@localhost shell]# ./shell8.sh
fengj : qing shuru 1
fur:qing shuru 2
buod:qing shuru 3
qing shuru xuanze2
fur jiagei ni
[root@localhost shell]# ./shell8.sh
fengj : qing shuru 1
fur:qing shuru 2
buod:qing shuru 3
qing shuru xuanze3
fuduo he wo zou
[root@localhost shell]# ./shell8.sh
fengj : qing shuru 1
fur:qing shuru 2
buod:qing shuru 3
qing shuru xuanze4
qing shuru zhengq xuanze

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326152728&siteId=291194637