一 、case中基本概述
1、什么是 case
case,语句和if类似,也是用来判断的,只不过当判断的条件较多时,使用case语句会if更加的方便
2、case 应用的场景
在生产环境中,我们总会遇到一个问题需要 根据不同的状况来执行不同的预案,那么我们要处理,这样的问题首先要根据可能出现的情况写出对应预案,然后根据选择来加载不同的预案。
比如服务的启停脚本,我们首先要写好启动、停止、重启的预案,然后根据不同的选择加载不同的预案。
3、case基础语法
case 变量 in
条件1)
执行的代码1
;;
条件2)
执行的代码2
;;
条件3)
执行的代码3
;;
*)
无匹配的命令序列
esac
场景实例
场景1:模拟安装不同版本的PHP服务
#!/usr/bin/bash
clear
cat << EOF
===================
1.Install php5.5
2.Install php5.6
3.Install php6.1
==================
EOF
read -p "请输入你需要安装的版本[1|2|3|4]:" Action
case $Action in
1)
echo "yum install php5.5"
;;
2)
echo "yum install php5.6"
;;
3)
echo "yum install php6.1"
;;
*)
echo "请真确的输入安装的版本序列号"
exit
esac
场景2:nginx 的启动、重启、停止脚本
#!/usr/bin/bash
Nginx_pid=/var/run/nginx.pid
read -p "请输入一下命令:[ start|resatrt|status|stop]:" Action
case $Action in
start)
if [ -f $Nginx_pid ];then
echo "nginx 已经启动"
else
systemctl start nginx
echo "nginx启动成功"
fi
;;
stop)
if [ ! -f $Nginx_pid ];then
echo "nginx 已经停止"
else
systemctl stop nginx
echo "nginx 停止成功"
fi
;;
status)
if [ -f $Nginx_pid ];then
echo "Nginx active (running)..."
else
echo "nginx 未启动"
fi
;;
restart)
if [ ! -f $Nginx_pid ];then
echo "nginx 已经停止"
else
systemctl restart nginx
echo "nginx 重启成功"
fi
;;
reload)
if [ -f $Nginx_pid ];then
nginx -t -c /etc/nginx/ngnix.conf
rc=$?
if [ $rc -eq 0 ];then
echo "nginx is reload "
else
ngx_conf=$(cat nginx.err |awk -F "[ :]" 'NR==1 {print $(NF-1)}')
ngx_line=$(cat nginx.err |awk -F "[ :]" 'NR==1 {print $NF}')
read -p "是否进入配置文件进行修改:" tt
case $tt in
y)
vim $ngx_conf +${ngx_line}
;;
n)
exit 1
esac
fi
else
echo "Nginx 没有启动"
fi
;;
*)
echo "USAGE: $0 [ start|resatrt|status|stop]"
exit 2
esac
场景3:nginx 状态监测脚本
`1、配置nginx的相关的配置文件`
cat /etc/nginx/conf.d/test.conf
server {
listen 80;
server_name test.cxp.com;
root /code;
location /{
index index.html;
}
location /nginx_status {
stub_status;
}
}
2、编写nginx的相关脚本
cat case_nginx_status.sh
#!/bin/bash
HostName=test.cxp.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}
case $1 in
active)
echo "$(( $(awk '/Active/ {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 '/Reading/ {print $2}' ${Nginx_status_file})"
;;
writing)
echo "$(awk '/Writing/ {print $4}' ${Nginx_status_file})"
;;
waiting)
echo "$(( $(awk '/Waiting/ {print $6}' ${Nginx_status_file})-1))"
;;
*)
echo "USAGE: $0 { active | accepts | handled | requests | reading | writing | waiting }"
exit 1
esac
场景4:使用case实现php-fpm状态监控脚本。
#php-fpm是一个管理器,管理PHP,而PHP是一个代码解析器
yum install nginx php php-fpm
cat /etc/php-fpm.d/www.conf
pm.status_path = /status
##################################################
cat /etc/nginx/conf.d/php.conf
server {
listen 80;
server_name php.cxp.com;
location /status {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}
}
HostName=php.cxp.com
php_status_file=phpfpm.status
php_status_path=phpfpm_status
curl -sH Host:${HostName} http://10.0.0.7/${php_status_path} > ${php_status_file}
case $1 in
pool)
echo "$(awk '/pool/ {print $NF}' ${php_status_file})"
;;
process_manager)
echo "$(awk '/process manager/ {print $NF}' ${php_status_file})"
;;
start_time)
echo "$(awk '/start time/{print $3,$4}' ${php_status_file})"
;;
start_since)
echo "$(awk '/start since/ {print $NF}' ${php_status_file})"
;;
accepted_conn)
echo "$(awk '/accepted conn/ {print $NF}' ${php_status_file})"
;;
listen_queue)
echo "$(sed -n '6p' ${php_status_file} |awk '{print $NF}')"
;;
max_listen_queue)
echo "$(awk '/max listen/ {print $NF}' ${php_status_file})"
;;
listen_queue_len)
echo "$(awk '/queue len/ {print $NF}' ${php_status_file})"
;;
idle_processes)
echo "$(awk '/idle processes/ {print $NF}' ${php_status_file})"
;;
active_processes)
echo "$(sed -n '10p' ${php_status_file} |awk '{print $NF}')"
;;
total_processes)
echo "$(awk '/total processes/ {print $NF}' ${php_status_file})"
;;
max_active_processes)
echo "$(awk '/max active processes/ {print $NF}' ${php_status_file})"
;;
max_children_reached)
echo "$(awk '/max children reached/ {print $NF}' ${php_status_file})"
;;
slow_requests)
echo "$(awk '/slow requests/ {print $NF}' ${php_status_file})"
;;
*)
echo "USAGE: $0 { pool | process_manager | start_time | start_since }"
exit 1
esac
参考实例
1.需求:根据用户输入的服务名称查询该服务的状态,并让用户选择启动、关闭、重启、保持不变并输出该服务器以启动、关闭、重启、保持不变
方案一:脚本如下
#!/bin/bash
#判断当前执行脚本的是否为超级管理员
if [ $UID -ne 0 ];then
echo "\"$USER\" $0 Permission denied"
exit
fi
#判断用户传入的参数
if [ $# -ne 1 ];then
echo "USAGE: $0 Service Name [ nginx | httpd | vsftpd | rsyncd ]"
exit
fi
systemctl status $1 &>/dev/null
if [ $? -eq 4 ];then
echo "Unit $1 could not be found."
else
#字符串比对
system_status=$(systemctl status $1|grep Active|awk '{print $2}')
if [ $system_status == "active" ];then
read -p "$1 已启动,你可以选择 [ restart | stop ] " Action
case $Action in
restart)
systemctl restart $1
echo "$1 重启成功......"
;;
stop)
systemctl stop $1
echo "$1 停止成功......"
;;
*)
exit 1
esac
#针对没有启动的服务,提示是否启动
elif [ $system_status == "inactive" ];then
read -p "$1 未启动,可以选择 [ start | quit ] " Action2
case $Action2 in
start)
systemctl start $1
echo "$1 启动成功"
;;
quit)
echo "Bye"
exit
;;
*)
exit
esac
fi
fi
方案二:脚本如下
#!/bin/bash
read -p "请输入你要查询服务的名称:" Action
systemctl status ${Action} &> /dev/null
if [ $? -eq 0 ];then
echo "Active: active (running)"
else
echo "Active: failed"
fi
cat <<EOF
1)启动
2)停止
3)重启
4)退出
EOF
read -p "请输入你需要执行的操作:[ 1 | 2 | 3 | 4 ] " Nu
case ${Nu} in
1)
systemctl start ${Action}
if [ $? -eq 0 ];then
echo "$Action服务已启动"
else
echo "$Action服务未启动"
fi
;;
2)
systemctl stop ${Action}
if [ $? -eq 0 ];then
echo "$Action服务已停止"
exit 1
fi
;;
3)
systemctl restart ${Action}
if [ $? -eq 0 ];then
echo "$Action服务已重启"
exit 1
fi
;;
4)
echo "抱歉,没有这个服务,你可以去其他地方找找"
esac
2.需求:取出当前系统日期、时间、当前有几个登陆用户、过去15分钟的平均负载、当前可用内存大小、当前系统空闲时间,输入到/tmp/txt.csv
total:服务器内存总大小
used:已经使用了多少内存
free:未被任何应用使用的真实空闲内存;
shared:被共享的物理内存;
buff/cache:缓冲、缓存区内存数,缓存在应用之中;
available:真正剩余的可被程序应用的内存数;
#!/usr/bin/bash
Date=$(date +%F_%T)
User=$(w|awk '/up/ {print $4}')
Load=$(w|awk '/up/ {print $NF}')
Keyong=$(free -h|awk '/Mem/ {print $NF}')
Kongxian=$(free -h|awk '/Mem/ {print $4}')
Time=$(cat /proc/uptime |awk '{print $2}')
echo "系统的日期时间 $Date"
echo "当前登录系统的用户数 $User"
echo "15分钟的平均负载 $Load"
echo "可用的内存大小 $Keyong MB"
echo "完全空闲的内存大小 $Kongxian MB"
echo "系统空闲的时间 $Time "