美文网首页
case 判断

case 判断

作者: 慕知 | 来源:发表于2020-12-25 16:30 被阅读0次
    • case判断适用的场景,if 都可以使用
    • if 单分支情况下 一般使用 case
    1,语法
    case $ in
    条件|条件)                   -------------> 相当于if语句中的 if
        判断
        ;;
    条件|条件|条件)                  -------------> 相当于if语句中的 elif
        判断
        ;;
    *)                     -------------> 相当于if语句中的 else
        判断
    esac                   -------------> 相当于if语句中的 fi
    
    2, 使用
    --
         输入喜欢的食物;
         当输入面条,显健康食品;
         输入巧克力,显示发胖食品;
         输入其他的,显示垃圾食品
    
    root@m01~]# vim /script/food.sh
    #!/bin/bash
    read -p "请输入喜爱的食物:面条 巧克力 辣条:     " food
    case $food in
    "面条")
        echo "健康食品"
        ;;
    "巧克力")
        echo "发胖食品"
        ;;
    *)
        echo "垃圾食品"
    esac
    
    [root@m01~]# chmod +x /script/food.sh
    [root@m01~]# /script/food.sh
    请输入喜爱的食物:面条 巧克力 辣条:     面条
    健康食品
    
    --- 案例 nginx源码包下载,脚本设置开启关闭命令 ---
    1, 源码安装nginx
    #官网下载源码包
    [root@\ web01~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
    [root@\ web01~]# ll
    -rw-r--r--  1 root root 1039530 2020-04-21 22:33 nginx-1.18.0.tar.gz
    # 解压
    [root@\ web01~]# tar xf nginx-1.18.0.tar.gz
    
    
    # 到加压包目录下编译,缺少依赖包
    [root@\ web01~]# cd nginx-1.18.0/
    [root@\ web01~/nginx-1.18.0]# ./configure --prefix=/usr/local/nginx
    checking for OS
     + Linux 3.10.0-957.el7.x86_64 x86_64
    checking for C compiler ... not found
    
    ./configure: error: C compiler cc is not found
    
    
    # 查看依赖工具包
    [root@\ web01~/nginx-1.18.0]# yum -y grouplist
    Loaded plugins: fastestmirror
    There is no installed groups file.
    Maybe run: yum groups mark convert (see man yum)
    Loading mirror speeds from cached hostfile
     * base: mirrors.aliyun.com
     * extras: mirrors.aliyun.com
     * updates: mirrors.aliyun.com
    Available Environment Groups:
       Minimal Install
       Compute Node
       Infrastructure Server
       File and Print Server
       Cinnamon Desktop
       MATE Desktop
       Basic Web Server
       Virtualization Host
       Server with GUI
       GNOME Desktop
       KDE Plasma Workspaces
       Development and Creative Workstation
    Available Groups:
       Cinnamon
       Compatibility Libraries
       Console Internet Tools
       Development Tools
       Educational Software
       Electronic Lab
       Fedora Packager
       General Purpose Desktop
       Graphical Administration Tools
       Haskell
       LXQt Desktop
       Legacy UNIX Compatibility
       MATE
       Milkymist
       Scientific Support
       Security Tools
       Smart Card Support
       System Administration Tools
       System Management
       TurboGears application framework
       Xfce
    Done
    
    
    # 安装依赖工具包
    [root@\ web01~/nginx-1.18.0]# yum -y groupinstall "Development Tools"
    
    [root@\ web01~/nginx-1.18.0]# ./configure --prefix=/usr/local/nginx
    
    ... ..../configure: error: the HTTP gzip module requires the zlib library.
    You can either disable the module by using --without-http_gzip_module
    option, or install the zlib library into the system, or build the zlib library
    statically from the source with nginx by using --with-zlib=<path> option.
    
    
    #根据提示继续下载依赖包
    [root@\ web01~/nginx-1.18.0]# yum install -y zlib*
    
    再次执行
    [root@\ web01~/nginx-1.18.0]# ./configure --prefix=/usr/local/nginx
    
    [root@\ web01~/nginx-1.18.0]# make && make install
    
    • 源码安装nginx 开启或关闭nginx使用命令/usr/local/nginx/sbin/nginx -s stop 等较麻烦
    2, 脚本编写
    [root@\ web01~]# vim /script/nginx.sh
    #!/bin/bash
    [ $# -ne 1 ] && echo "Usage: $0 {start|stop|restart|reload|status}" && exit
    case $1 in
    "start")
        echo "nginx is ready to start"
        /usr/local/nginx/sbin/nginx &> /dev/null
        ps -ef | grep [n]ginx | grep master &> /dev/null
        if [ $? -eq 0 ];then
            echo "nginx has benn started"
        else
            /user/local/nginx/sbin/nginx &> /dev/null
            if [ $? -eq 0 ];then
                echo "nginx start succeed"
            else
               echo "failed to start nginx"
            fi
        fi
        ;;
    "stop")
        echo "nginx is ready to stop"
        /usr/local/nginx/sbin/nginx -s stop
        if [ $? -eq 0 ];then
            echo "nginx has been stopped"
        else
            echo "failed to stop nginx"
        fi
        ;;
    "restart")
        /usr/local/nginx/sbin/nginx -s stop
        if [ $? -eq 0 ];then
            echo "nginx has been stopped"
        else
            echo "faild to stop nginx"
            /usr/local/nginx/sbin/nginx -s stop
            if [$? -ne 0 ];then
                echo "nginx has been started"
            else
                echo "failed to stop nginx"
            fi
        fi
        ;;
    
    "status")
        ps -ef | grep [n]ginx | grep master &> /dev/null
        if [ $? -eq 0 ];then
            echo "nginx is up"
        else
            echo "nginx is down"
        fi
        ;;
        esac
    
    3,验证
    [root@\ web01~]# chmod +x /script/nginx.sh
    
    [root@\ web01~]# /script/nginx.sh status
    nginx is down
    
    [root@\ web01~]# /script/nginx.sh start
    nginx is ready to start
    nginx has benn started
    
    [root@\ web01~]# /script/nginx.sh stop
    nginx is ready to stop
    nginx has been stopped
    
    [root@\ web01~]# /script/nginx.sh restart
    nginx has been stopped
    succeed to start nginx
    
    4,优化脚本
    [root@\ web01~]# vim /script/nginx.sh
    !/bin/bash
    [ $# -ne 1 ] && echo "Usage: $0 {start|stop|restart|status}" && exit
    function start_nginx(){
        ps -ef | grep [n]ginx | grep master &> /dev/null
        if [ $? -eq 0 ];then
            echo "nginx has benn started"
        else
            /usr/local/nginx/sbin/nginx &> /dev/null
            if [ $? -eq 0 ];then
                echo "nginx start succeed"
            else
               echo "failed to start nginx"
            fi
        fi
    }
    function stop_nginx(){
        ps -ef | grep [n]ginx | grep master &> /dev/null
        if [ $? -ne 0 ];then
            echo "nginx has been stopped"
        else
            /usr/local/nginx/sbin/nginx -s stop &> /dev/null
            ps -ef | grep [n]ginx | grep master &> /dev/null
            if [ $? -ne 0 ];then
                echo "succeed to stop nginx"
            else
                echo "failed to stop nginx"
            fi
        fi
    }
    
    case $1 in
    "start")
        start_nginx
        ;;
    "stop")
        stop_nginx
        ;;
    "restart")
        stop_nginx
        start_nginx
        ;;
    
    "status")
        ps -ef | grep [n]ginx | grep master &> /dev/null
        if [ $? -eq 0 ];then
            echo "nginx is up"
        else
            echo "nginx is down"
        fi
        ;;
        esac
    
    
    
    ##注意:用函数function(下几篇会单独讲到)方便调用
           nginx_stop环节加一行sleep 1代码;
           该命令执行需要时间,如果nginx是开启状态,执行 /usr/local/nginx/sbin/nginx -s stop,  立刻执行开启nginx,很有可能$?是0,但是其实没有重启,是本身就开启的状态
    
    

    相关文章

      网友评论

          本文标题:case 判断

          本文链接:https://www.haomeiwen.com/subject/mmcmnktx.html