加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Shell语法—— case 条件语句

发布时间:2020-12-15 23:23:51 所属栏目:安全 来源:网络整理
导读:case 条件语句语法 case 条件语句语法格式为: case " 变量 " in 值 1) 指令 1 ;; 值 2) 指令 2 ;; * ) 指令 3 ;;esac 了解即可 给字体加颜色的命令: 例:echo -e "E[1;31m 红颜色 hello world E[0m" E 等同于 33 "[1" 数字 1 表示加粗显示 31m 表示
case 条件语句语法

case 条件语句语法格式为:

case  " 变量 " in
                    值 1)
                                    指令 1
                    ;;
                    值 2)
                                    指令 2
                    ;;
                    * )
                                    指令 3
                    ;;
esac
了解即可

给字体加颜色的命令:
例:echo -e "E[1;31m 红颜色 hello world E[0m"

  1. E 等同于 33
  2. "[1" 数字 1 表示加粗显示
  3. 31m 表示红色字体
  4. "[0m" 表示关闭所有属性
  5. "[1m" 表示设置高亮度
  6. "[4m" 表示下划线
  7. "[5m" 表示闪烁
  8. "[7m" 表示反显
  9. "[8m" 表示消隐
  10. 33[30m -- 33[37m 表示设置前景色
  11. 33[40m -- 33[47m 表示设置背景色
    案例一:
    编写 Nginx 启动 停止服务。
    此处仅为做逻辑与 case 语法练习所用,脚本本身并没有什么用途
#!/bin/bash
RETVAL=0
pid=/var/run/nginx.pid
. /etc/init.d/functions
start(){
if [ ! -f $pid ];then
        service nginx start
        RETVAL=$?
        if [ $RETVAL -eq 0 ];then
                action "nginx is started" /bin/true
                return $RETVAL
        else
                action "nginx is started" /bin/false
                return $RETVAL
        fi
else
        echo "nginx is running"
        return 0
fi
}

stop(){
if [ -f $pid ];then
        service nginx stop
        RETVAL=$?
        if [ $RETVAL -eq 0 ];then
                action "nginx is started" /bin/true
                return $RETVAL
        else
                action "nginx is started" /bin/false
                return $RETVAL
        fi
else 
        echo "nginx is stop"
        return 0
fi
}

restart(){
        stop
        start
}

case $1 in
  start|yes)
        start
        RETVAL=$?
        ;;
  stop|no)
        stop
        RETVAL=$?
        ;;
  restart|or)
        restart
        RETVAL=$?
        ;;
  *)
        echo "Usage:$0{start(yse)|stop(on)|restart(or)}"
        exit 1
esac
exit $RETVAL

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读