shell脚本之case的用法及示例
case基础语法
和if很像
case 变量名4 in
模式匹配1)
命令的集合
;;
模式匹配2)
命令的集合
;;
模式匹配3)
命令的集合
;;
*) *的下一行不需要有;;
echo USAGE[$0 1|2|3]
esac
[root@web scripts]# cat case.sh
#!/bin/sh
case $1 in
Linux)
echo linux......
;;
Shell)
echo Shell......
;;
MySQL)
echo MySQL.....
;;
*)
echo "USAGE $0 [Linux|Shell|MySQL]"
esac
case 批量删除用户
[root@web scripts]# cat casedel.sh
#!/bin/sh
read -p "请输入用户名前缀: " prefix
read -p "请输入要删除几个用户: " num
for i in `seq $num`
do
echo $prefix$i
done
read -p "你确定要删除以上用户吗?[y|yes|YES|n|N|no]" ready
for n in `seq $num`
do
name=$prefix$n
case $ready in
y|yes|YES)
id $name &>/dev/null
if [ $? -eq 0 ];then
userdel -r $name
[ $? -eq 0 ] && echo "$name del is ok"
else
echo "id: $name: no such user"
fi
;;
n|N|no)
echo "不删除我玩啥呢?" && exit
;;
*)
echo "USAGE $0 [y|yes|YES|n|N|no]"
esac
done
case案例实现
案例一:使用case实现nginx服务启停脚本。
[root@manager case]# cat case-4.sh
#!/bin/bash
# Author: Oldux.com QQ: 552408925
# Date: 2019-10-30
# FileName: case-4.sh
# URL: https://www.xuliangwei.com
# Description:
source /etc/init.d/functions
nginx_pid=/var/run/nginx.pid
case $1 in
start)
if [ -f $nginx_pid ];then
if [ -s $nginx_pid ];then
action "Nginx 已经启动" /bin/false
else
rm -f $nginx_pid
systemctl start nginx &>/dev/null
if [ $? -eq 0 ];then
action "Nginx启动成功" /bin/true
else
action "Nginx启动失败" /bin/false
fi
fi
else
systemctl start nginx &>/dev/null
if [ $? -eq 0 ];then
action "Nginx启动成功" /bin/true
else
action "Nginx启动失败" /bin/false
fi
fi
;;
stop)
if [ -f $nginx_pid ];then
systemctl stop nginx && \
rm -f $nginx_pid
action "Nginx is stopped" /bin/true
else
echo "[error] open() "$nginx_pid" failed (2: No such file or directory)"
fi
;;
status)
if [ -f $nginx_pid ];then
echo "nginx (pid $(cat $nginx_pid)) is running..."
else
echo "nginx is stopped"
fi
;;
reload)
echo "reload"
;;
*)
echo "USAGE: $0 {start|stop|status|restart}"
exit 1
esac
[root@manager case]#
案例二:使用case实现nginx状态监控脚本。 stub_status
[root@manager case]# cat case-5.sh
#!/bin/bash
#********************************************************************
#Author: 一个shell小白
#QQ: 2226823216
#Date: 2019-10-30
#FileName: case-5.sh
HostName=test.oldxu.com
Nginx_status_file=nginx.status
Nginx_Status_Path=nginx_status
curl -sH Host:${HostName} http://127.0.0.1/${Nginx_Status_Path} >${Nginx_status_file}
i
case $1 in
active)
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}) ))
;;
Writing)
echo $(( $(awk 'NR==4 {print $4}' ${Nginx_status_file}) ))
;;
Waiting)
echo $(( $(awk 'NR==4 {print $6}' ${Nginx_status_file}) ))
;;
*)
echo "USAGE: $0 { active | accepts | handled | requests | Reading | Writing | Waiting }"
exit 1
esac
案例三:需求3:使用case实现php-fpm状态监控脚本。
[root@web01 php-fpm.d]# cd /etc/php-fpm.d/
[root@web01 php-fpm.d]# ls
www.conf
[root@web01 php-fpm.d]# vim www.conf
添加如下一行代码
pm.status_path = /php_fpm_status
[root@web01 php-fpm.d]# systemctl restart php-fpm
[root@web01 conf.d]# cat oldxu.com.conf
server {
listen 80;
server_name test.oldxu.com;
location / {
index index.heml;
}
location /nginx_status {
stub_status;
}
location /php_fpm_status {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 conf.d]# curl -sH Host:test.oldxu.com http://127.0.0.1:80/php_fpm_status
pool: www
process manager: dynamic
start time: 30/Oct/2019:21:37:59 +0800
start since: 79
accepted conn: 1
listen queue: 0
max listen queue: 0
listen queue len: 128
idle processes: 4
active processes: 1
total processes: 5
max active processes: 1
max children reached: 0
slow requests: 0
[root@web01 case]# cat var01.sh
#!/bin/bash
Hostname=test.oldxu.com
Php_status=php_fpm_status
Php_backss=Php_backss
curl -sH Host:${Hostname} http://127.0.0.1:80/${Php_status} >${Php_backss}
case $1 in
pool)
echo $(( $(awk 'NR==1 {print $NF}' ${Php_backss}) ))
;;
listenqueuelen)
echo $(( $(awk 'NR==8 {print $NF}' ${Php_backss}) ))
;;
*)
echo "USAGE: $0 {pool | listenqueuelen}"
exit
esac
技术是没有终点的,也是学不完的,最重要的是活着、不秃。 学习看书还是看视频,都不重要,重要的是学会,欢迎关注,我们的目标---不秃。
---更多运维开发交流及软件包免费获取请加V: Linuxlaowang