shell脚本—case用法

文章目录

  • shell脚本—case用法
    • 1.什么是case?
    • 2.case使用场景
    • 3.case语法

1.什么是case?

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

2.case使用场景

在生产环境中,我们总会遇到一个问题需要根据不同的状况来执行不同的预案,那么我们要处理这样的问题首先要根据可能出现的情况写出对应预案,然后根据选择来加载不同的预案。

比如服务器启停脚本,我们首先要写好启动,停止,重启的预案,然后根据不用的选择加载不同的预案

3.case语法

case 变量 in
条件 1)执行代码块1;;
条件 2)执行代码块2;;
条件 3)执行代码块3;;
*)无匹配后命令序列esac
#示例1:输入1备份,输入2copy,输入3退出,输入其他的提醒不要乱输入
[root@manage case]# cat case01.sh
#!/bin/bash
cat <<eof
****************
** 1. backup  **
** 2. copy    **
** 3. quit    **
****************
eof
read -p "请输入要执行的编号:[ 1 | 2 | 3 ]:" Action
case $Action in 1|backup|BACKUP)echo "backup.....";;2|copy|COPY)echo "copy........";;3|quit|QUIT)exit;;*)echo "USAGE:$0 请不要乱输入...."esac
#示例2:输入1.输出5.5,输入2输出7.0,输入3输出7.5版本,输入4退出,输入5提示不要乱输入
[root@manage case]# cat case02.sh
#!/bin/bash
clear
cat <<EOF
=====================================
1) Installed PHP 5.5
2) Installed PHP 7.0
3) Installed PHP 7.3
4) quit
=====================================
EOF
read -p "请输入你要安装的版本[ 1 | 2 | 3 | 4 ]: " Actioncase $Action in1)echo "Installed PHP 5.5";;2)echo "Installed PHP 6.5";;3)echo "Installed PHP 7.5";;4)exit;;*)echo "AUSAGE: $0 请不要乱输入...."exitesac
#需求3:用case实现nginx启停脚本
[root@manage case]# cat case03.sh
#!/bin/bash
source /etc/init.d/functions
nginx_pid=/var/run/nginx.pidcase $1 instart)if [ -f $nginx_pid ];thenif [ -s $nginx_pid ];thenaction "Nginx 已经启动" /bin/falseelserm -f $nginx_pidsystemctl start nginx &>/dev/nullif [ $? -eq 0 ];thenaction "Nginx启动成功" /bin/trueelseaction "Nginx启动失败" /bin/falsefifielsesystemctl start nginx &>/dev/nullif [ $? -eq 0 ];thenaction "Nginx启动成功" /bin/trueelseaction "Nginx启动失败" /bin/falsefifi;;stop)if [ -f $nginx_pid ];thensystemctl stop nginx && \rm -f $nginx_pidaction "Nginx is stopped" /bin/trueelseecho "[error] open() "$nginx_pid" failed (2: No such file or directory)"fi;;status)if [ -f $nginx_pid ];thenecho "nginx (pid  $(cat $nginx_pid)) is running..."elseecho "nginx is stopped"fi;;reload)#1.nginx没有启动直接报错#2.nginx启动了,先检查配置文件语法#如果nginx语法没问题,则reload#如何nginx语法有问题#提示用户是否进入对应的错误行数修改 [ y | n ]# y 进入修改# n 退出操作if [ -f $nginx_pid ];thennginx -t -c /etc/nginx/nginx.conf &>nginx.errrc=$?if [ $rc -eq 0 ];thenaction "Nginx is Reloaded" /bin/trueelsengx_conf=$(cat nginx.err |awk -F "[ :]" 'NR==1 {print $(NF-1)}')ngx_line=$(cat nginx.err |awk -F "[ :]" 'NR==1 {print $NF}')read -p "是否进入 ${ngx_conf} 配置文件的 ${ngx_line} 行修改: [ y | n ] " ttcase $tt iny)vim $ngx_conf +${ngx_line};;n)exit 1esacfielseaction "Nginx 没有启动" /bin/falsefi;;*)echo "USAGE:  $0 {start|stop|status|restart}"exit 1
esac[root@manage case]# sh case04.sh status
nginx (pid  ) is running...
[root@manage case]# sh case04.sh stop
Nginx is stopped                                           [  OK  ]
[root@manage case]# sh case04.sh reload
Nginx 没有启动                                             [FAILED]
[root@manage case]# sh case04.sh start
Nginx启动成功                                              [  OK  ]
[root@manage case]# sh case04.sh reload
Nginx is Reloaded                                          [  OK  ]
#需求4:使用case实现nginx状态监控脚本。 stub_status
sh nginx_status.sh
USAGE nginx_status.sh { Active | accepts | handled | requests | Reading  | Writing  |Waiting }1.nginx开启状态监控[root@manager case]# cat /etc/nginx/conf.d/test.conf server {listen 80;server_name test.ttt.com;location / {index index.html;}location /nginx_status {stub_status;}}2.awk取值     curl -H Host:test.ttt.com http://127.0.0.1:80/nginx_status3.case[root@manage case]# cat case05.sh
#!/bin/bash
HostName=test.ttt.com
Nginx_status_file=nginx.status
Nginx_Status_Path=nginx_statuscurl -sH Host:${HostName} http://127.0.0.1/${Nginx_Status_Path} >${Nginx_status_file}
case $1 inactive)echo $(( $(awk '/Active connections: / {print $NF}' ${Nginx_status_file}) -1 ));;accepts)echo $(( $(awk 'NR==3 {print $1}' ${Nginx_status_file}) -1 ));;handled)echo $(( $(awk 'NR==3 {print $2}' ${Nginx_status_file}) -1 ));;requests)echo $(( $(awk 'NR==3 {print $3}' ${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 "USAGE: $0 { active | accepts | handled | requests | Reading  | Writing  | Waiting }"exit 1
esac[root@manage case]# sh case05.sh active
0
[root@manage case]# sh case05.sh accepts
10
[root@manage case]# sh case05.sh handled
11
[root@manage case]# sh case05.sh requests
12
#需求5:case实现php状态监控脚本  php_status
[root@manage case]# yum install php-fpm php pho -y
[root@manage case]# vim /etc/php-fpm.d/www.conf
pm.status_path = /php_status
[root@manage case]# systemctl start php-fpm[root@manage case]# cat /etc/nginx/conf.d/test.conf
server {listen 80;server_name test.ttt.com;location / {index index.html;}location /nginx_status {stub_status;}location /php_status {fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}
}[root@manage case]# systemctl restart nginx  php-fpms[root@manage case]# cat case06.sh
#!/bin/bash
case $1 inaccepted)echo "$(( $(awk '/accepted conn/ {print $NF}' php.status)))";;listen)echo "$(( $(awk 'NR==6 {print $NF}' php.status)))";;idle)echo "$(( $(awk '/idle/ {print $NF}' php.status)))";;active)echo "$(( $(awk '/^active/ {print $NF}' php.status)))";;total)echo "$(( $(awk '/total/ {print $NF}' php.status)))";;slow)echo "$(( $(awk '/slow/ {print $NF}' php.status)))";;*)echo "[ESAUG: accepted  | listen | idle | active | total |  | slow ]"exit 1esac
#需求6:编写脚本,根据用户输入的服务名称查询该服务的状态,并让用户选择启动、关闭、重启、保持不变并输出改服务器以启动、关闭、重启、保持不变1.接收用户输入的服务名称   $1
2.根据用户传入的$1 查询服务对应的状态
3.针对已启动的,重启或关闭
4.针对未启动,启动或停止[root@manage case]# cat case07.sh
#!/bin/bash
#判断用户传入的参数
if [ $# -ne 1 ];thenecho "USAGE: server name [nginx | httpd | vsftpd | php-fpm ]"exit
fi#当前执行脚本的是否为超级管理员
if [ $UID -ne 0 ];thenecho "\"$USER\" $0 不是超级管理员"exit
fi
systemctl status $1 &>/dev/nullif [ $? -eq 4 ];thenecho "Unit $1 could not be found"else#字符串比对system_status=$(systemctl status $1 | grep Active | awk '{print $2}')if [ $system_status == "active" ];thenread -p "$1 已启动,可以选择[ restart | stop ]:" Actioncase $Action inrestart)systemctl restart $1 >/dev/nullecho "$1重启成功.....";;stop)systemctl stop $1 >/dev/nullecho "$1 停止成功.....";;*)exit 1esacelif [ $system_status == "inactive" ];thenread -p "$1 未启动,可以选择 [ start | quit ]:" Actioncase $Action instart)systemctl start $1echo "$1启动成功";;quit)echo "退出"exit;;*)exitesacfi
fi[root@manage case]# sh +x case07.sh nginx
nginx 未启动,可以选择 [ start | quit ]:start
nginx启动成功
[root@manage case]# sh +x case07.sh nginx
nginx 已启动,可以选择[ restart | stop ]:stop
nginx 停止成功.....

shell脚本—case用法相关推荐

  1. linux shell之case用法

    #!/bin/bash                    # #case用法,用变量来匹配某值,如果匹配成功则执行它下面的命令,直到 ::为止 a=20                       ...

  2. Shell 脚本基本用法

    文章目录 基础 解释器 扩展名 运行 shell 注释 特性 变量 定义 赋值 调用变量 {} 只读变量 readonly 删除变量 变量类型 字符串 关于引号 字符串操作 数组 定义 读取数组 获取 ...

  3. linux sh文件case,Shell脚本case语句简明教程

    Shell case语句为多选择语句.可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令.case语句格式如下: case 值 in 模式1) command1 command2 ...

  4. shell脚本case传递参数

    #!/bin/bash case $1 in cjk) echo "Hello,cjk" ;; bdyjy) echo "hello,bdyjy" ;; *) ...

  5. Linux shell脚本---case语句

    一,case语句解释 1,case语句的作用 case语句用于条件判断,判断一个变量的不同取值 2,case语句的语法 case 变量值 in 匹配模式1) 命令序列1 ;;匹配模式2) 命令序列2 ...

  6. linux中的shell脚本case,【shell】Linux shell 之 case 详解

    总的来说,case是一个判断语句 ,比if更加容易理解一点. case 语句格式 case in 变量 值1) 内容 ;; 值2) 内容 ;; esac 注意:每个内容后面都需要添加 ;; ,可以跨行 ...

  7. Shell脚本基本用法

    结合日常的使用场景,持续补充~ 1.出现问题立刻停止方法 set命令的-e参数,linux系统自带的说明是:"Exit immediately if a simple command exi ...

  8. shell脚本逻辑判断,文件目录属性判断,if,case用法

    shell脚本中的逻辑判断 1.if then fi [root@weixing01 shell]# cat if1.sh #!/bin/bash a=5 if [ $a -gt 3 ] thenec ...

  9. 【Linux】30.ssh不用手动输入密码登录终端sshpass 和 shell脚本后跟参数自动匹配case的用法

    ssh不用手动输入密码登录终端sshpass 和 case的组合用法 1.sshpass 的用法 在第一次手动输入密码ssh mdc@172.16.34.17 登陆上终端后,可以把下面两条语句做成sh ...

最新文章

  1. Python-memcached的基本使用
  2. 散列表查找的一个实例
  3. 常用公差配合表图_涨知识!常用的机械测量工具,你都知道吗?
  4. boost::function_types::is_function_pointer用法的测试程序
  5. git项目根据不同需求进行独立开发
  6. shell export 作用
  7. OpenShift 4 之 GitOps(2)用ArgoCD部署应用
  8. 百度大脑语音能力引擎论坛定档 11.28,邀你一同解码 AI 语音的奥秘
  9. Linux --- 常用命令
  10. 世界各国国家代码简称
  11. 免费主机,免费二级域名分发,免费建临时网站,免费扒网
  12. $('xx')[0].files[0]的意思
  13. RabbitMQ 使用规范
  14. 可视化任务编排拖拉拽的数据集成工具
  15. 转载:WVGA,QVGA,VGA,HVGA,WQVGA是什么意思?如何区别?
  16. windows下安装虚拟机
  17. Android系统的指纹开发
  18. 【技术分享】美团外卖的商业变现的技术思考和实践
  19. 计算机二级考试c语言冲刺,计算机二级考试试题C语言冲刺试题
  20. 如何激活win10家庭版?这个方法超管用

热门文章

  1. html space空格符
  2. 磁盘删除分区(还原)方法
  3. linux下adb传输文件,使用adb在电脑和手机间传文件,adb手机传文件
  4. android实现抖音列表,Android使用RecyclerView实现抖音主界面
  5. 青少年软件编程C++一级题库(11-21)
  6. ArcGIS:如何利用模型构建器(ModelBuilder)解决基于人口和已有商业点的商业连锁店选址问题?
  7. js字符串字母大小写转换
  8. Anchor是什么?
  9. Ubuntu系统设置开机进引导菜单界面
  10. uin-app 使用canvas画简易海报