shell

  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
原文地址:https://www.cnblogs.com/Afrafre/p/11405720.html