shell中的case判断

格式
case  变量名 in 
    value1)
        command
        ;;
    value2)
        command
        ;;
    value3|value4)
        command
        ;;
    *)
        command
        ;;
esac
举例
"$1"表示输入的第1个参数
"$2"表示第2个参数
#!/bin/bash
case  $1 in 
    start)
        echo "starting..."
        ;;
    stop)
        echo "stopping..."
        ;;
    reboot|restart)
        echo "restarting..."
        ;;
    *)
        echo "Usage:{start|stop|restart|reboot}"
        ;;
esac
[root@localhost wang]# sh -x case.sh
+ case $1 in
+ echo 'Usage:{start|stop|restart|reboot}'
Usage:{start|stop|restart|reboot}
[root@localhost wang]# sh -x case.sh start
+ case $1 in
+ echo starting...
starting...
[root@localhost wang]# 

[root@localhost wang]# sh -x case.sh
+ case $2 in
+ echo 'Usage:{start|stop|restart|reboot}'
Usage:{start|stop|restart|reboot}
[root@localhost wang]# sh -x case.sh start
+ case $2 in
+ echo 'Usage:{start|stop|restart|reboot}'
Usage:{start|stop|restart|reboot}
[root@localhost wang]# sh -x case.sh start stop
+ case $2 in
+ echo stopping...
stopping...
[root@localhost wang]# 
case后面也可以直接跟参数
[root@localhost wang]# sh -x case.sh
+ case "start" in
+ echo starting...
starting...
[root@localhost wang]# sh -x case.sh stop
+ case "start" in
+ echo starting...
starting...
[root@localhost wang]# 
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 13,864评论 6 13
  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 11,132评论 0 10
  • 第 2 章 SHELL 基础知识2.1 shell脚本我们在上面简单介绍了一下什么是shell脚本,现在我们来进一...
    LiWei_9e4b阅读 5,457评论 0 0
  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 7,920评论 2 9
  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 9,947评论 0 5