linux12shell编程 -->流程控制之case
2021/6/16 7:21:40
本文主要是介绍linux12shell编程 -->流程控制之case,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
文章目录
- 流程控制之case语句
- 一 、语法
- 二、 案例
- 案例1
- 案例2:编写nginx启动脚本
- 案例3:编写nginx启动脚本
- 案例4:编写一个简易跳板机脚本
流程控制之case语句
一 、语法
case 变量 in 模式1) 命令序列1 ;; 模式2) 命令序列2 ;; 模式3) 命令序列3 ;; *) 无匹配后命令序列 esac
二、 案例
案例1
[root@openvpn day3]# cat check_role.sh #! /bin/ read -p "please input you name:" name if [ -z "$name" ];then name="default" fi case $name in "root") echo "管理员" ;; "mm") echo "普通" ;; "default") echo "默认用户" ;; *) echo "防客" esac
案例2:编写nginx启动脚本
[root@egon shell]# cat nginx_stat.sh #!/bin/ . /etc/init.d/functions if [ $# -ne 1 ] then echo "USAGE $0 {start|stop|restart}" exit 1 fi if [ "$1" == "start" ] then action "start nginx" /bin/true elif [ "$1" == "stop" ] then action "stop nginx" /bin/true elif [ "$1" == "restart" ] then action "restart nginx" /bin/true else echo "USAGE $0 {start|stop|restart}" exit 1 fi [root@egon shell]# chmod +x nginx_stat.sh [root@egon shell]# ./nginx_stat.sh start start nginx [ 确定 ] [root@egon shell]# ./nginx_stat.sh restart restart nginx [ 确定 ] [root@egon shell]# ./nginx_stat.sh USAGE ./nginx_stat.sh {start|stop|restart}
案例3:编写nginx启动脚本
# 储备知识1 netstat -lntup|grep ":80\b" # \b锚定单词的结尾 # 储备知识2 action:打印一段信息并执行给定的命令,然后根据给定命令的执行的结果来调用 success,failure方法,确定最终显示的内容 [root@egon shell]# action "nginx start is" : nginx start is [ 确定 ] [root@egon shell]# action "nginx start is" /bin/true nginx start is [ 确定 ] [root@egon shell]# action "nginx start is" /bin/false nginx start is [失败] # 代码 [root@egon shell]# cat nginx_stat.sh #!/bin/ . /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) netstat -an | grep -i Listen | grep -q "\b80\b" if [ $? -eq 0 ] then echo "Nginx is runing..." else /usr/sbin/nginx fun fi ;; stop) /usr/sbin/nginx -s stop fun ;; reload) /usr/sbin/nginx -s reload fun ;; restart) netstat -lntup|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 -lntup|grep ":80\b" &>/dev/null if [ $? -eq 0 ] then echo "Nginx is runing ..." else echo "Nginx is not runing ..." fi ;; *) echo "Usage: $0 {start|stop|status|restart|reload}" exit 2 esac
案例4:编写一个简易跳板机脚本
# 储备知识 Linux中断信号区别为:键入不同、对应操作不同、启用不同。 1、HUP中断信号:HUP中断信号的对应操作为让进程挂起,睡眠。同<Ctrl+X> 2、INT中断信号:INT中断信号的对应操作为正常关闭所有进程。同<Ctrl+C> 3、TERM中断信号 15:TERM中断信号的对应操作为正常的退出进程。 4、KILL中断信号 9:KILL中断信号的对应操作为强制关闭进程。 5、STOP 19暂停(同 Ctrl + Z) 6、CONT 18继续(与STOP相反, fg/bg命令) 7、TSTP中断信号:TSTP中断信号的对应操作为暂时停用进程。 # 代码 [root@egon shell]# cat jumpserver.sh #!/bin/ cat<<EOF 1. BACKUP 10.0.0.41 2. WEB02 192.168.12.21 3. WEB03 10.0.0.9 EOF trap "echo 不要乱按键盘,否则服务器将会爆炸" HUP INT TSTP while true do read -p "请输入连接主机编号信息: " num read -p "请输入账号: " user # read -p "请输入要执行的命令: " cmd case $num in 1) ssh $user@10.0.0.41 [ $? -ne 0 ] && echo "connect faild" ;; 2) ssh $user@192.168.12.21 [ $? -ne 0 ] && echo "connect faild" ;; *) echo "请输入连接主机信息" esac done
这篇关于linux12shell编程 -->流程控制之case的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-11Linux部署Scrapy学习:入门级指南
- 2024-09-11Linux部署Scrapy:入门级指南
- 2024-08-21【Linux】分区向左扩容的方法
- 2024-08-21【Linux】gnome桌面环境切换KDE Plasma
- 2024-08-19如何安装 VMware Tools (macOS, Linux, Windows)
- 2024-08-15Linux部署Scrapy教程:入门级指南
- 2024-07-29linux命令行下好用的性能监控工具atop
- 2024-07-04Linux系统上离线升级SSH服务的具体操作步骤-icode9专业技术文章分享
- 2024-06-0600-macOS和Linux安装和管理多个Python版本
- 2024-03-30[译]漫画SELinux概念