首页 > 系统服务 > 详细

【shell】Linux shell 之 case 详解

时间:2018-03-25 21:10:49      阅读:167      评论:0      收藏:0      [点我收藏+]

总的来说,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

【shell】Linux shell 之 case 详解

原文:http://blog.51cto.com/xiaowangzai/2090947

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!