53.shell脚本之case语句

mac2024-03-14  31

1.什么是case 2.csse使用场景 3.case基础语法 4.case脚本案例

PHP-FPM状态监控

需求1:使用case实现nginx服务启停脚本。

[root@manager case]# cat case-2.sh #!/bin/bash #Date: 2019-10-30 #FileName: case-2.sh #Description: #定义环境变量 . /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服务已停止" /bin/true else echo "${nginx_pid} : No such file or directory" fi ;; status) if [ -f $nginx_pid ];then echo "PID $(cat $nginx_pid) is active..." else echo "${nginx_pid}不存在,服务未启动" fi ;; reload) if [ -f $nginx_pid ];then nginx -t -c /etc/nginx/nginx.conf &> nginx.error rc=$? if [ $rc -eq 0 ];then action "nginx is reload" /bin/true else nginx_conf=$(cat nginx.error |awk -F "[ :]" 'NR==1 {print $(NF-1)}') nginx_line=$(cat nginx.error |awk -F "[ :]" 'NR==1 {print $NF}') read -p "是否进入${nginx_conf} 配置文件中的 ${nginx_line} 行修改: [ yes | no ]" select case $select in yes) vim ${nginx_conf} +${nginx_line} ;; no) exit 2 esac fi else action "nginx 没有启动" /bin/false fi ;; *) echo "USAGE: $0 {start | stop | status | reload }" exit 3 esac

需求2:使用case实现nginx状态监控脚本。 stub_status

#!/bin/bash #Date: 2019-10-30 #FileName: case-3.sh #Description: HostName=test.cheng.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 '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 $NF}' ${Nginx_status_file}) -1 )) ;; *) echo "USAGE: $0 { active | accepts | handled | requests | reading | writing | waiting }" exit 1 esac 需求3:使用case实现php-fpm状态监控脚本。 [root@web01 conf.d]# cat test.cheng.com.conf server { listen 80; server_name test.cheng.com; location ~ ^/(phpfpm_status)$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } [root@manager case]# cat case-4.sh #!/bin/bash #Date: 2019-10-30 #FileName: case-4.sh #Description: HostName=test.cheng.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:编写脚本,根据用户输入的服务名称查询该服务的状态,并让用户选择启动、关闭、重启、保持不变并输出该服务器以启动、关闭、重启、保持不变

[root@manager case]# cat case-6.sh #!/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 方式二: [root@manager case]# cat case-5.sh #!/bin/bash #Date: 2019-10-30 #FileName: case-5.sh #Description: 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:输入两个数字,输出的两个数字加减乘除得四个的数(判断输入的是否为两个数字,输入的是否为数字)

[root@manager case]# cat case-6.sh #!/bin/bash #Date: 2019-10-30 #FileName: case-6.sh #Description: if [[ ! $1$2 =~ ^[0-9]+$ ]];then echo "你输入的不是数字" exit 1 fi if [ $# -ne 2 ];then echo "请输入两个数字:[ 1 | 2 ]" exit 1 fi echo "$1 + $2 = $[ $1 + $2 ]" echo "$1 - $2 = $[ $1 - $2 ]" echo "$1 * $2 = $[ $1 * $2 ]" echo "$1 / $2 = $[ $1 / $2 ]" echo "$1 % $2 = $[ $1 % $2 ]"

4:取出当前系统日期、时间、当前有几个登陆用户、过去15分钟的平均负载、当前可用内存大小、当前系统空闲时间,输入到/tmp/txt.csv

[root@manager 作业题]# cat if2.sh #!/bin/bash #Date: 2019-10-30 #FileName: if2.sh #Description: Date=$(date +%F_%T) User=$(w |awk '/up/ {print $6}') Load=$(w |awk '/up/ {print $NF}') Free=$(free -h |awk '/Mem/ {print $NF}') Time=$(cat /proc/uptime |awk '{print $2}') echo "当前系统时间:${Date}" > /tmp/txt.csv echo "当前登录用户:${User}" >> /tmp/txt.csv echo "当前系统过去15分钟的平均负载:${Load}" >> /tmp/txt.csv echo "当前系统可用内存大小:${Free}" >> /tmp/txt.csv echo "当前系统空闲时间:${Time}" >> /tmp/txt.csv

5:检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不是,则提示您非管理员

[root@manager 作业题]# cat if1.sh #!/bin/bash #Date: 2019-10-30 #FileName: if1.sh #Description: if [ $UID -eq 0 ];then echo "用户为超级管理员" yum install vsftpd -y &> /dev/null else echo "抱歉,您不是管理员" fi

3:查看当前系统已使用内存的百分比,如果以使用的内存占比超过50%则将后台运行的服务列出来,并让用户选择保持不变或者关掉某个服务,后再次显示使用内存的百分比,若还超过50%,则重复上述操作

最新回复(0)