day58-linux-shell-case

day 58 linux-case

1.case基本概述

  1. 什么是case

case语句和if类似,也是用来判断的,只不过当判断的条件较多时,使用case语句会比if更加的方便.

  1. case使用场景

在生产环境中,需要根据不同的状况来执行不同的预案,这样的问题首先要解决可能出现的情况写出预案,然后根据不同的选择来加载不同的预案.

  1. case基础语法
bash
case 变量 in
条件 1)
      执行代码块1
      ;;
条件 2)
      执行代码块2
      ;;
条件 3)
      执行代码块3
      ;;
*)
      无匹配后命令
esac
  1. case脚本案例
bash 
[root@manager-61 ~/case]#cat case01.sh 
#!/bin/bash
#*********************
#Author: 流星花雨
#QQ: 1679338722
#Date: 2019-10-30
#FileName: case01.sh
#URL: https://www.leitiancheng.cn
#Description: The test script
#*********************
cat <<EOF
****************
**  1. backup **
**  2. copy   **
**  3. quit   **
****************
EOF
read -p "请输入编号: [ 1 | 2 | 3 ]" Action
case $Action in
     1)
        echo "Backup..."
    ;;
     2)
        echo "Copy..."
        ;;
     *)
    echo "请输入正确的数字!"
        ;;
esac

需求一: 使用case实现nginx服务启停脚本

bash
[root@manager-61 ~/case]#cat nginx.sh 
#!/bin/bash
#*********************
#Author: 流星花雨
#QQ: 1679338722
#Date: 2019-10-30
#FileName: nginx.sh
#URL: https://www.leitiancheng.cn
#Description: The test script
#*********************
source /etc/init.d/functions
nginx_pid=/var/run/nginx.pid
case $1 in
    start)
        if [ -f ${nginx_pid} ];then
           if [ -s ${nginx_pid} ];then
              action "nginx 已启动" /bin/false
           else 
              rm -rf ${nginx_pid}
              systemctl start nginx >/dev/null
            if [ $? -eq 0];then
               action "nginx 启动成功" /bin/true
            else
               action "nginx 启动失败" /bin/false
            fi
          fi
            else
             systemctl start nginx
                if [ $? -eq 0 ];then
                           action "nginx 启动成功" /bin/true
            else
               action "nginx 启动失败" /bin/false
            fi
        fi
        ;;
    stop)
        if [ -f ${nginx_pid} ];then
           systemctl stop nginx
           rm -rf ${nginx_pid} 
           action "nginx 已停止" /bin/true
        else
           echo "[error] open() "$nginx_pid" failed (2: No such file or directory)"        

        fi
        ;;
    reload)
            if [ -f ${nginx_pid} ];then
           nginx -t -c /etc/nginx/nginx.conf &> nginx.err
           if [ $? -eq 0 ];then
            action "检测与法成功" /bin/true
           else
                nginx_conf=$(cat nginx.err |awk -F "[ :]" 'NR==1 {print $(NF-1)}')
            nginx_line=$(cat nginx.err |awk -F "[ :]" 'NR==1 {print $NF}')
            read -p " ${nginx_conf} 语法错误,是否进入文件 ${nginx_line} 行进行修改: " err
            case $err in
                y)
                  vim ${nginx_conf} +${nginx_line}
                ;;
                n)
                 exit
                ;;

            esac
           
           fi
                else
               action "nginx 未启动" /bin/false
        fi
        ;;
    status)
        if [ -f ${nginx_pid} ];then
                   echo "nginx (pid  $(cat $nginx_pid)) is running..."
           else
               echo "nginx is stopped"
            fi
            ;;
       *)
                echo "USAGE:  $0 {start|stop|status|reload}"
        exit 1
                ;;
esac

需求二:使用case实现nginx状态监控脚本.

[root@manager-61 ~/case]#cat nginx_status.sh 
#!/bin/bash
#*********************
#Author: 流星花雨
#QQ: 1679338722
#Date: 2019-10-30
#FileName: nginx_status.sh
#URL: https://www.leitiancheng.cn
#Description: The test script
#*********************
Hostname=test.ltc.com
nginx_status_file=nginx.status
nginx_status_path=nginx_status
curl -sH Host:${Hostname} http://127.0.0.1/${nginx_status_path} > ${nginx_status_file}
case $1 in
    active)
          echo $(( $(awk 'NR==1 {print $NF}' ${nginx_status_file}) -1 ))
          ;;
    accepts)
          echo $(( $(awk 'NR==3 {print $(NF-2)}' ${nginx_status_file}) -1 ))
          ;;
    handled)
          echo $(( $(awk 'NR==3 {print $(NF-1)}' ${nginx_status_file}) -1 ))
          ;;
    requests)
          echo $(( $(awk 'NR==3 {print $NF}' ${nginx_status_file}) -1 ))
          ;;
    reading)
          echo $(( $(awk 'NR==4 {print $2}' ${nginx_status_file}) -1 ))
          ;;
    writing)
          echo $(( $(awk 'NR==4 {print $4}' ${nginx_status_file}) -1 ))
          ;;
    waiting)
          echo $(( $(awk 'NR==4 {print $6}' ${nginx_status_file}) -1 ))
          ;;
    *)
          echo "请输入查看的状态: [ active | accepts | handled | requests | reading  | writing  |waiting ]"
          exit
          ;;
esac

需求3:使用case实现php-fpm状态监控脚本。

[root@manager-61 ~/case]#cat php_status.sh 
#!/bin/bash
#*********************
#Author: 流星花雨
#QQ: 1679338722
#Date: 2019-10-30
#FileName: php_status.sh
#URL: https://www.leitiancheng.cn
#Description: The test script
#*********************
Hostname=test1.ltc.com
php_status_file=php.status
php_status_path=status
curl -sH Host:${Hostname} http://127.0.0.1/${php_status_path} >${php_status_file}
case $1 in
    since)
          echo $(( $(awk 'NR==4 {print $NF}' ${php_status_file}) -1 ))
          ;;
    accepted)
          echo $(( $(awk 'NR==5 {print $NF}' ${php_status_file}) -1 ))
          ;;
    *)
          echo "请输入想要查看的状态: [ since | accepted ]"
          exit
          ;;
esac

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值