shell - case 流程控制


  1. case 實現程序流程的選擇,循環

# 服務的腳本啟動和停止 ---- case 的應用場景
case variables in 
variables 1)
	order 1;;
variables 2)
	order 2;;
variables 3)
	order 3;;
*)
	no matches
esac
# 注意 if不能同時判斷用戶輸入為 1 或者是 backup 

# 使用case 判斷用戶輸入,注意結尾用 ;;

#!/usr/bin/bash
cat <<eof
####################
1. backup
2. copy
3. quit
####################
eof
read -p "please enter [1|2|3]:" re
case $re in
1|backup)   # 這里可以同時判斷輸入的是1還是backup , 區別於if 判斷
        echo "backup";;
2|copy)
        echo "copy";;
3|quit)
        echo "quit" && exit;;
*)
        echo "attention your input!!!"
        echo "USAGE: $0 {1|2|3}"
esac

一個rsync 的啟動和停止的腳本

# 注意腳本名 不能用rsync , 這樣取進程號的時候才不會取出多個
1. 如何啟動命令 rsync --daemon
ps aux|grep rsync|grep -v grep
2. 如何停止    pkill rsync
# $$ 當前腳本運行時的進程號

source /etc/init.d/functions
# 傳入第一個參數
rs=$1
if [ $rs == 'start' ];then
        if [ ! -f /var/run/rsync.pid ];then
                touch /var/run/rsync.pid
                rsync --daemon
                # 注意使用action ,true 為綠色,false為紅色
                action 'rsync is starting' /bin/true
        else
                action 'rsync already started' /bin/false
        fi
elif [ $rs == 'stop' ];then
        if [ ! -f /var/run/rsync.pid ];then
                action 'async is already stopped' /bin/false
        else
                rm -f /var/run/rsync.pid
                pkill rsync
                action 'rsync is stopping' /bin/true
        fi
elif [ $rs == 'status' ];then
	if [ ! -f /var/run/rsync.pid ];then
		echo 'Rsync service status inactive'
	else
		echo $$
		rsync_pid=$$
		rsync_status=$(ps aux|grep rsync|grep -v grep|grep -v $rsync_pid|awk '{print $2}')
		echo 'rsync service status active $rsync_status'
	fi
else
        echo 'USAGE: $0 {start|stop}'
        exit
fi
###################### 使用case 來完成服務的啟動和停止 ################################

#!/usr/bin/bash
source /etc/init.d/functions
rs=$1
case $rs in 
	start)
		#先判斷文件是否存在
		if [ ! -f /var/run/rsync.pid ];then
			touch /var/run/rsync.pid
			rsync --daemon
			action "rsync starting..." /bin/true
		else
			action "rsync already started" /bin/false
		fi 	
		;;
	stop)
		# 文件如果不存在已經停了
		if [ ! -f /var/run/rsync.pid ];then
			action "rsync is already stopped" /bin/false
		else
			action "rsync is stopping" /bin/true
			rm -f /var/run/rsync.pid
			pkill rsync
		fi
		;;
	status)
		echo "enter status"
		# 判斷文件存在的話輸出pid
		if [ ! -f /var/run/rsync.pid ];then
			echo " rsync service status inactive"
		else
			Rsync_status=$(ps aux|grep rsync|grep -v grep|awk '{print $2}')
            # 注意 不能單引號套雙引號,這樣$Rsync_status 原樣輸出
			echo "rsync service status active  '$Rsync_status' " 
		fi
		;;
	*)
		echo "USAGE $0 {start|stop|status}"
		exit
esac 

nginx 啟動和停止腳本

# 如何啟動 /usr/sbin/nginx
# 如何停止 /usr/sbin/nginx -s stop
# 如何重載 /usr/sbin/nginx -s reload

#!/usr/bin/bash
source /etc/init.d/functions
# 要加鎖,腳本運行過程中不能重復執行
if [ -f /tmp/nginx.lock ];then
	echo "腳本正在運行,請稍后" 
	exit
fi
touch /tmp/nginx.lock
# 開始正文
rs=$1
case $rs in 
	start)
		if [ -f /var/run/nginx.pid ];then
			action " already started" /bin/false
			exit
		else
			/usr/sbin/nginx
			action " start successfully" /bin/true
		fi
		;;
	stop)
		if [ -f /var/run/nginx.pid ];then
			/usr/sbin/nginx -s stop
			if [ $? -eq 0 ];then
				action "stop successfully" /bin/true
			else
				action "stop failed" /bin/false
			fi
		else
			action "already stopped" /bin/false
		fi
		;;
	reload)
		# 如果文件存在,可以重載
		if [ -f /var/run/nginx.pid ];then
			/usr/sbin/nginx -s reload
			if [ $? -eq 0 ];then
				action "reload successly" /bin/true
			else
				action " reload failded" /bin/false
			fi
			exit
		else
			action " service already stopped" /bin/false
		fi
		;;
	status)
		if [ -f /var/run/nginx.pid ];then
			nginx_pid=$(cat /var/run/nginx.pid)
			echo "nginx $nginx_pid is running"
		else
			echo "nginx is not running "
		fi		
		;;
	*)
		echo "USAGE: $0 [ start | stop | relaod | status ] "
esac
# 腳本運行結束后解鎖
rm -f /tmp/nginx.lock
########################################### 修改版#######################################
#!/usr/bin/bash
source /etc/init.d/functions
# 要加鎖,腳本運行過程中不能重復執行
if [ -f /tmp/nginx.lock ];then
	echo "腳本正在運行,請稍后" 
	exit
fi
touch /tmp/nginx.lock
rs=$1
case $rs in 
	start)
		if [ -f /var/run/nginx.pid ];then
			action " already started" /bin/false
			exit
		else
			/usr/sbin/nginx
			action " start successfully" /bin/true
			
		fi
		rm -f /tmp/nginx.lock
		;;
	stop)
		if [ -f /var/run/nginx.pid ];then
			/usr/sbin/nginx -s stop
			if [ $? -eq 0 ];then
				action "stop successfully" /bin/true
				
			else
				action "stop failed" /bin/false
			fi
		else
			action "already stopped" /bin/false
		fi
		rm -f /tmp/nginx.lock
		;;
	reload)
		# 如果文件存在,可以重載		
		if [ -f /var/run/nginx.pid ];then
			# 判斷命令是否執行有錯,如果有錯取出錯誤的行
			/usr/sbin/nginx -t &>/dev/null
			if [ $? -eq 0 ];then
				/usr/sbin/nginx -s reload
				if [ $? -eq 0 ];then
					action "reload successly" /bin/true
				else
					action " reload failded" /bin/false
				fi
			else
				/usr/sbin/nginx -t &>err.txt
				nginx_conf=$(awk -F "[: ]" 'NR==1 {print $(NF-1)}' err.txt)
				nginx_line=$(awk -F "[: ]" 'NR==1 {print $(NF)}' err.txt)
				reap -p "是否進入配置文件進行修改?" re
				case $re in 
					y|yes|YES)
						vim +${nginx_line} $nginx_conf
						;;
					n|no|NO)
						echo "modify manully"
						exit
						;;
					*)
						echo "USAGE: $0 {y|n}"
				esac
			fi

			exit
		else
			action " service already stopped" /bin/false
		fi
		rm -f /tmp/nginx.lcok
		;;
	status)
		if [ -f /var/run/nginx.pid ];then
			nginx_pid=$(cat /var/run/nginx.pid)
			echo "nginx $nginx_pid is running"
		else
			echo "nginx is not running "
		fi
		rm -f /tmp/nginx.lock
		;;
	*)
		echo "USAGE: $0 [ start | stop | relaod | status ] "
esac
# 腳本運行結束后解鎖
rm -f /tmp/nginx.lock

系統管理工具箱


case 實行簡單的JumpServer

1. 執行腳本后,需要看到所有能管理的主機
2. 選擇菜單,提示輸入連接某個主機

#!/usr/bin/bash

# 定義一個函數可以調用多次
meminfo(){
	cat <<-EOF
	******************
	1. lb01-172.16.1.5
	2. lb02-172.16.1.6
	3. web01-172.16.1.8
	h. help
	******************
	EOF
}
meminfo
trap "" HUP INT TSTP   # ctrl c v不起作用
read -p "please enter the number you want: " connection
case $connection in 
	1)
		ssh root@lb01-172.16.1.5
		;;
	2)
		ssh root@lb02-172.16.1.6
		;;
	3)
		ssh root@web01-172.16.1.8
		;;
	h)
		clear
		meminfo
		;;
	*)
		echo "USAGE: $0 輸入 1,2,3,h"
esac
'''
可以加死循環,保證程序連接后端服務,退出后還能選擇主機
可以將腳本加入到 /etc/bashrc中,用戶一連接,就自動運行該腳本
'''
命令總結:
# 創建用戶
useradd octivia
# 設置密碼
echo "123"|passwd --stdin octivia

多級菜單

# 函數調用
#!/usr/bin/bash
installfunc(){
cat <<-EOF
1. 安裝nginx
2. 安裝php
3. 退出
EOF
}
nginxfunc(){
cat <<-EOF
1. 安裝版本1
2. 安裝版本2
3. 清屏
4. 返回上一頁面
EOF
}
phpfunc(){
cat <<-EOF
1. 安裝版本一
2. 安裝版本二
3. 退出
EOF
}
installfunc

read -p "請選擇:" software
case $software in
        1)
    		   clear
                nginxfunc
                read -p "please choose version: " version
                case $version in
                        1)
                                echo "install nginx version1"
                                ;;
                        2)
                                echo "install nginx version2"
                                ;;
                        3)
                                clear ;;
                        4)
                                installfunc
                esac
                ;;
        2)
                phpfunc
                read -p "please choose version: " version
                case $version in
                        1)
                                echo "install php version1"
                                ;;
                        2)
                                echo "install php version2"
                                ;;
                        3)
                                installfunc
                esac
                ;;
        3)
                exit
esac


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM