在shell 脚本中,除了用if来判断逻辑外,还有一种常用的方式,那就是case了。具体格式为:
case 变量in
value1)
command
;;
value2)
command
;;
value3)
command
;;
*)
command
;;
esac
举例:
[[email protected] ~]# cat case.sh
#/bin/bash
read -p "input a number:" n
a=$[$n%2]
case $a in
1)
echo "the unm is odd"
;;
0)
echo "the unm is even"
;;
*)
echo "it is not a num"
;;
esac
时间: 2024-10-12 08:54:57