-
case 实现程序流程的选择,循环
case variables in
variables 1)
order 1;;
variables 2)
order 2;;
variables 3)
order 3;;
*)
no matches
esac
cat <<eof
1. backup
2. copy
3. quit
eof
read -p "please enter [1|2|3]:" re
case $re in
1|backup)
echo "backup";;
2|copy)
echo "copy";;
3|quit)
echo "quit" && exit;;
*)
echo "attention your input!!!"
echo "USAGE: $0 {1|2|3}"
esac
一个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 '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
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"
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}')
echo "rsync service status active '$Rsync_status' "
fi
;;
*)
echo "USAGE $0 {start|stop|status}"
exit
esac
nginx 启动和停止脚本
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
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. 选择菜单,提示输入连接某个主机
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
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
多级菜单
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!