流程控制之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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值