1.流程控制语句之case语句
case主要作用是对程序的选择,循环等操作 #语法: case 变量 in 变量值1) 命令序列 ;; #命令序列结束符 变量值2) 命令序列 ;; 变量值3) 命令序列 ;; 变量值N) 命令序列 ;; *) #不符合上面所有条件 命令序列 exit #或者其他命令 esac #语法示例 安装不同PHP的版本 1. 菜单, PHP版本的菜单 2. 提示用户根据菜单进行选择安装不同的PHP版本 3. 根据用户选择进行安装不同的PHP版本 [root@shell01 scripts]# vim case-1.sh #!/bin/bash #1.准备PHP的版本菜单 cat<<EOF ########################## 1.安装PHP-5.5版本 2.安装PHP-5.6版本 3.安装PHP-7.1版本 4.退出当前安装 ########################## EOF #2.提示用户根据菜单进行选择安装不同的PHP版本 read -p "请根据上方菜单进行选择安装不同的PHP版本[1|2|3|4]: " Install #3.根据用户的选择,进行安装不同的版本 case $Install in 1) echo "你闲着了安装PHP-5.5版本........." echo "正在安装PHP-5.5版本,请稍后......" sleep 3 #等待3秒 echo "PHP-5.5版本安装成功............." ;; 2) echo "你闲着了安装PHP-5.6版本........." echo "正在安装PHP-5.6版本,请稍后......" sleep 3 #等待3秒 echo "PHP-5.6版本安装成功............." ;; 3) echo "你闲着了安装PHP-7.1版本........." echo "正在安装PHP-7.1版本,请稍后......" sleep 3 #等待3秒 echo "PHP-7.1版本安装成功............." ;; 4) echo "你没有选择安装任何PHP版本! 程序退出!" exit ;; *) echo "你的选择不符合要求!请根据上方菜单进行选择[1|2|3|4]." exit esac
2.case语句之场景示例(rsync)
服务启动停止脚本
1.编写一个rsync的启动停止脚本 实现: start stop status restart 1.如何启动rsync /usr/bin/rsync --daemon 2.如何停止rsync pkill rsync #注意: 千万不要使用rsync作为脚本的名字 3.参考系统中其他服务的pid文件,我们自己创建 #(这里不是用system管理,无法用system status去查看) [root@shell01 scripts]# vim case-2.sh #!/bin/bash #1.引用函数库 [ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!" #2.判断位置变量的个数是否只有一个 if [ $# -ne 1 ];then echo "Usage: $0 {start|stop|status|restart}" exit fi #3.编写case语句选项 Pid_File=/var/run/rsync.pid #这里启动不会自动创建pid文件,需要脚本自行创建 case $1 in start) if [ -f $Pid_File ];then action "服务Rsync正在运行中......" /bin/true exit else #当服务要启动的时候,需要手动创建pid文件 touch $Pid_File && /usr/bin/rsync --daemon &>/dev/null && sleep 3 #给3秒启动时间 if [ $? -eq 0 ];then action "服务Rsync启动成功....." /bin/true else action "服务Rsync启动失败....." /bin/false fi fi ;; stop) if [ -f $Pid_File ];then #当服务在启动的状态下,pid文件是存在的,如果需要停止服务,同样需要把pid文件删除 rm -f $Pid_File && pkill rsync && sleep 2 if [ $? -eq 0 ];then action "服务Rsync停止成功......" /bin/true else action "服务Rsync停止失败......" /bin/false fi else action "服务Rsync不在运行中......" /bin/true exit fi ;; status) #当pid文件存在时,则服务在运行中,pid文件不存在时,服务不在运行中 if [ -f $Pid_File ];then action "服务Rsync正在运行中......" /bin/true else action "服务Rsync不在运行中......" /bin/true fi ;; restart) #重启,当服务正在运行中,需要停止服务再启动服务,当服务不在运行中,需要直接启动 if [ -f $Pid_File ];then #停止服务 rm -f $Pid_File && pkill rsync && sleep 2 if [ $? -eq 0 ];then action "服务Rsync停止成功......" /bin/true else action "服务Rsync停止失败......" /bin/false fi #启动服务 touch $Pid_File && /usr/bin/rsync --daemon &>/dev/null && sleep 3 if [ $? -eq 0 ];then action "服务Rsync启动成功....." /bin/true else action "服务Rsync启动失败....." /bin/false fi action "服务Rsync重启成功...." /bin/true else action "服务Rsync不在运行中......" /bin/true #启动服务 touch $Pid_File && /usr/bin/rsync --daemon &>/dev/null && sleep 3 if [ $? -eq 0 ];then action "服务Rsync启动成功....." /bin/true else action "服务Rsync启动失败....." /bin/false fi action "服务Rsync重启成功...." /bin/true fi ;; *) echo "Usage: $0 {start|stop|status|restart}" exit esac
3.Nginx的启动停止脚本
start stop status restart reload 1.怎么启动Nginx /usr/sbin/nginx 2.怎么停止Nginx /usr/sbin/nginx -s stop 3.怎么进行重启Nginx 先stop 再start 4.怎么进行平滑重启Nginx /usr/sbin/nginx -s reload 5.在什么情况下,才能进行平滑重启 只有在Nginx服务在运行的时候,才能进行平滑重启。如果Nginx服务没有在运行中,是不能进行平滑重启的。 ----------------------------------- [root@shell01 scripts]# vim case-3.sh #!/bin/bash #1.引用函数库 [ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!" #2.判断位置变量是否存在且只有一个 if [ $# -ne 1 ];then echo "Usage: $0 {start|stop|status|restart|reload}" exit fi #3.编写case语句 Pid_File=/var/run/nginx.pid case $1 in start) if [ -f $Pid_File ];then action "Nginx服务正在运行中......." /bin/true else /usr/sbin/nginx &>/dev/null && sleep 2 if [ $? -eq 0 ];then action "Nginx服务启动成功......" /bin/true else action "Nginx服务启动失败......" /bin/false fi fi ;; stop) if [ -f $Pid_File ];then /usr/sbin/nginx -s stop &>/dev/null && sleep 2 if [ $? -eq 0 ];then action "Nginx服务停止成功......" /bin/true else action "Nginx服务停止失败......" /bin/false fi else action "Nginx服务不在运行中......" /bin/true fi ;; status) if [ -f $Pid_File ];then action "Nginx服务正在运行中......" /bin/true else action "Nginx服务不在运行中......" /bin/true fi ;; restart) if [ -f $Pid_File ];then #先停止服务 /usr/sbin/nginx -s stop &>/dev/null && sleep 2 if [ $? -eq 0 ];then action "Nginx服务停止成功...." /bin/true else action "Nginx服务停止失败...." /bin/false fi #启动服务 /usr/sbin/nginx &>/dev/null && sleep 2 if [ $? -eq 0 ];then action "Nginx服务启动成功......" /bin/true action "Nginx服务重启成功......" /bin/true else action "Nginx服务启动失败......" /bin/false fi else action "Nginx服务不在运行中....." /bin/false #启动服务 /usr/sbin/nginx &>/dev/null && sleep 2 if [ $? -eq 0 ];then action "Nginx服务启动成功......" /bin/true action "Nginx服务重启成功......" /bin/true else action "Nginx服务启动失败......" /bin/false fi fi ;; reload) if [ -f $Pid_File ];then /usr/sbin/nginx -s reload &>/dev/null && sleep 2 if [ $? -eq 0 ];then action "Nginx服务平滑重启成功......" /bin/true else action "Nginx服务平滑重启失败......" /bin/false fi else action "Nginx服务没有在运行中,无法进行平滑重启...." /bin/false fi ;; *) echo "Usage: $0 {start|stop|status|restart|reload}" exit esac -----------------------------------------
#测试 [root@shell01 scripts]# sh case-3.sh Usage: case-3.sh {start|stop|status|restart|reload} [root@shell01 scripts]# sh case-3.sh start Nginx服务正在运行中....... [ OK ] [root@shell01 scripts]# sh case-3.sh stop Nginx服务停止成功...... [ OK ]
4.加锁机制
#加锁机制 当这个脚本被某一个人执行的时候,别人是无法执行的,只能等待执行结束之后,才能执行 #优化脚本(通过创建文件,判断文件是否存在来实现锁的功能) ----------------------------------------------- [root@shell01 scripts]# vim case-3.sh #!/bin/bash #1.引用函数库 [ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!" #2.判断位置变量是否存在且只有一个 if [ $# -ne 1 ];then echo "Usage: $0 {start|stop|status|restart|reload}" exit fi #2.5 进行加锁机制 Suo_File=/tmp/nginx.lock if [ -f $Suo_File ];then echo "此脚本$0 正在运行中,请稍后再执行......" exit fi #2.6 加锁 touch $Suo_File &>/dev/null #3.编写case语句 Pid_File=/var/run/nginx.pid case $1 in start) if [ -f $Pid_File ];then action "Nginx服务正在运行中......." /bin/true else /usr/sbin/nginx &>/dev/null && sleep 2 if [ $? -eq 0 ];then action "Nginx服务启动成功......" /bin/true else action "Nginx服务启动失败......" /bin/false fi fi ;; stop) if [ -f $Pid_File ];then /usr/sbin/nginx -s stop &>/dev/null && sleep 2 if [ $? -eq 0 ];then action "Nginx服务停止成功......" /bin/true else action "Nginx服务停止失败......" /bin/false fi else action "Nginx服务不在运行中......" /bin/true fi ;; status) if [ -f $Pid_File ];then action "Nginx服务正在运行中......" /bin/true else action "Nginx服务不在运行中......" /bin/true fi ;; restart) if [ -f $Pid_File ];then #先停止服务 /usr/sbin/nginx -s stop &>/dev/null && sleep 2 if [ $? -eq 0 ];then action "Nginx服务停止成功...." /bin/true else action "Nginx服务停止失败...." /bin/false fi #启动服务 /usr/sbin/nginx &>/dev/null && sleep 2 if [ $? -eq 0 ];then action "Nginx服务启动成功......" /bin/true action "Nginx服务重启成功......" /bin/true else action "Nginx服务启动失败......" /bin/false fi else action "Nginx服务不在运行中....." /bin/false #启动服务 /usr/sbin/nginx &>/dev/null && sleep 2 if [ $? -eq 0 ];then action "Nginx服务启动成功......" /bin/true action "Nginx服务重启成功......" /bin/true else action "Nginx服务启动失败......" /bin/false fi fi ;; reload) if [ -f $Pid_File ];then /usr/sbin/nginx -s reload &>/dev/null && sleep 2 if [ $? -eq 0 ];then action "Nginx服务平滑重启成功......" /bin/true else action "Nginx服务平滑重启失败......" /bin/false fi else action "Nginx服务没有在运行中,无法进行平滑重启...." /bin/false fi ;; *) echo "Usage: $0 {start|stop|status|restart|reload}" # 这里删掉exit esac #4.解锁(上方不要出现exit) rm -f $Suo_File &>/dev/null ---------------------------------------- #测试 [root@shell01 scripts]# sh case-3.sh stop Nginx服务停止成功...... [ OK ] #在另一个窗口同时执行脚本 [root@shell01 scripts]# sh case-3.sh stop 此脚本case-3.sh 正在运行中,请稍后再执行......
标签:case,bin,shell,服务,......,Nginx,加锁,action,true From: https://www.cnblogs.com/ludingchao/p/18204673