流程控制之case语句

Posted 我的紫霞辣辣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了流程控制之case语句相关的知识,希望对你有一定的参考价值。

case语句

语法

case 变量 in
模式1)
	命令序列1
	;;
模式2)
	命令序列2
	;;
模式3)
	命令序列3
	;;
*)
	无匹配后命令序列
esac

判断是当前登陆用户是否是管理员身份

#!/bin/bash

systemctl status nginx
case `id -u` in					# root用户id -u,结果为0
        0)
                echo "root用户"
                ;;
        *)
                echo "不是root用户"
esac

案例:编写nginx启动脚本

写法一、if 判断写法

#!/bin/bash

read -p "请输入你想要执行的操作(start/stop/restart)" nginx

if [[ $nginx == "start" ]];then
        systemctl start nginx
        if [ $? -eq 0 ];then
                echo "nginx开启成功"
        else
                echo "nginx启动失败"
        fi
elif [[ $nginx == "stop" ]];then
        systemctl stop nginx
        if [ $? -eq 0 ];then
                echo "nginx关闭成功"
        else
                echo "nginx关闭失败"
        fi
elif [[ $nginx == "restart"]];then
        if [ $? -eq 0 ];then
                echo "nginx重启成功"
        else
                echo "nginx重启失败"
        fi
else
        echo "输入正确的指令"
fi

写法二、case语句

#!/bin/bash

read -p "请输入你想要执行的操作,输入id或者指令(1.start/2.stop/3.restart)" nginx

case $nginx in
        1|start)
        systemctl start nginx &> /dev/null
        if [ $? -eq 0 ];then
                echo "nginx开启成功"
        else
                echo "nginx启动失败"
        fi
        ;;
        2|stop)
        systemctl stop nginx &> /dev/null
        if [ $? -eq 0 ];then
                echo "nginx关闭成功"
        else
                echo "nginx关闭失败"
        fi
        ;;
        3|restart)
        systemctl restart nginx &> /dev/null
        if [ $? -eq 0 ];then
                echo "nginx重启成功"
        else
                echo "nginx重启失败"
        fi
        ;;
        *)
        echo "输入正确的指令"
esac

写法三、case语句+if判断+函数

[root@m01 ~]# netstat -lntp | grep ":22\\b"				# \\b锚定单词的结尾
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1375/sshd           
tcp6       0      0 :::22                   :::*                    LISTEN      1375/sshd          

action:打印一段信息并执行给定的命令,然后根据给定命令的执行的结果来调用 success,failure方法,确定最终显示的内容
[root@m01 ~]# . /etc/init.d/functions
[root@m01 ~]# action "nginx start is"
nginx start is                                             [  OK  ]
[root@m01 ~]# action "nginx start is" /bin/true
nginx start is                                             [  OK  ]
[root@m01 ~]# action "nginx start is" /bin/false
nginx start is                                             [FAILED]
#!/bin/bash

. /etc/init.d/functions
args=$1

fun(){
        [ $? -eq 0 ] && action "nginx $args is " /bin/true || echo "nginx $args is" /bin/false  
}

case $1 in
        start)
                # grep -q 安静模式,不打印任何标准输出,如果有匹配的内容则立即返回状态值0
                netstat -an | grep -i Listen | grep -q "\\b80\\b"
                if [ $? -eq 0 ];then
                        echo "nginx is running..."
                else
                        /usr/sbin/nginx
                        fun
                fi
                ;;
        stop)
                usr/sbin/nginx -s stop
                fun
                ;;
        reload)
                usr/sbin/nginx -s reload
                fun
                ;;
        restart)
                netstat -lntp | grep ":80\\b" &> /dev/null
                if [ $? -ne 0 ]
                then
                        /usr/sbin/nginx
                        [ $? -eq 0 ] && echo "nginx start is ok " || echo "nginx start is failed"
                else
                        /usr/sbin/nginx -s stop
                        [ $? -eq 0 ] && echo "nginx stop is ok " || echo "nginx stop is failed"
                        sleep 2
                        /usr/sbin/nginx
                        fun
                fi
                ;;
      status)
                netstat -lntp | grep ":80\\b" &>/dev/null
                if [ $? -eq 0 ]
                then
                        echo "nginx is running..."
                echo
                        echo "nginx is not running..."
                fi
                ;;
        *)
                echo "输入错误"
                exit 888
esac

案例:编写一个简易跳板机脚本

该脚本需要先配置好公钥与私钥才可以使用

#!/bin/bash

while true
do
        read -p "请输入账号:" user
        read -p "请输入连接主机的ip地址:" id
        read -p "请输入要执行的命令:" cmd

case $id in
        192.168.15.7)
                ssh $user@$id $cmd
                if [ $? -eq 0 ];then
                        echo "web01命令执行成功"
                        break
                else
                        echo "web01命令执行失败,重新输入"
                fi
                ;;
        192.168.15.8)
                ssh $user@$id $cmd
                if [ $? -eq 0 ];then
                        echo "web02命令执行成功"
                        break
                else
                        echo "web02命令执行失败,重新输入"
                fi
                ;;
        *)
                echo "请输入正确的主机信息!!!,重新输入"
esac
done

以上是关于流程控制之case语句的主要内容,如果未能解决你的问题,请参考以下文章

MySQL基础--08---流程控制( IF CASE) 和 游标

Linux之Shell编程(10)--Shell流程控制语句实例演示

流程控制语句-switch选择语句

Puppet 之 流程控制介绍

常见的流程控制语句

java 流程控制基础之if else