总的来说,case是一个判断语句 ,比if更加容易理解一点。

case 语句格式

case in 变量 
值1)
	内容 ;;
值2)
	内容 ;;
esac

注意:每个内容后面都需要添加 ;; ,可以跨行也可以同行写。

实例:根据用户输入的选择执行语句。

#!/bin/bash -
# 打印选择菜单
cat <<EOF
        Option:
        1) restart networking service.
        2) start networking service.
        3) stop networking service.
        *) exit.
EOF
read -p "Please enter a option:" number

# 使用case语句对参数进行判断
case $number in
1)
        echo "The Networking service is restart,wait......" ;;
2)
        echo "The Networking service is start,wait......" ;;
3)
        echo "The Networking service is stop,wait......" ;;
*)
        echo "exit."  ;;
esac

[root@XiaoPeng scripts]# bash case.sh Option: 1) restart networking service. 2) start networking service. 3) stop networking service. *) exit. Please enter a option:1 The Networking service is restart,wait......

版权所有:arppinging