=======================================================================
张贺,多年互联网行业工作经验,担任过网络工程师、系统集成工程师、LINUX系统运维工程师
笔者微信:zhanghe15069028807,现居济南历下区
=======================================================================
case
case其实和if很像,本质是一样的,只不过case更严格一些 ,case的主要应用场景在服务和脚本的启停,使用方法如下:
case 变量 in
变量 1)
命令序列;;
变量 2)
命令序列;;
变量 3)
命令序列;;
*)
无匹配后命令序列;;
esac
例1:
#!
#!/bin/bash
cat <<eof
****************
** 1. backup **
** 2. copy **
** 3. quit **
****************
eof
read -p "请输入你想操作的选项[1|2|3]: " re
if [ $re -eq 1 ] -o [ $re == "backup" ];then #不能这么用,会出错,用case就不会出错
echo "------BackupDone....."
elif [ $re -eq 2 ];then
echo "------CopyDone....."
elif [ $re -eq 3 ];then
echo "你选择了退出...."
exit
else
echo "注意点....."
fi
改进例1:
#!
#!/bin/bash
cat <<eof
****************
** 1. backup **
** 2. copy **
** 3. quit **
****************
eof
read -p "请输入你想操作的选项[1|2|3]: " re
case $re in
1|backup|BACKUP) #这么用没有问题
echo "backup..."
;;
2)
echo "Copy...."
;;
3)
echo "quit" && exit
;;
*)
echo "你注意点..."
echo "USAGE: $0 {1|2|3}"
exit
esac
rsync
rsync、sesync、tomcat这样的服务不能使用systemctl控制,我们这里通过rsync来举例子。
#!/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 Service Running..." /bin/false
fi
;;
stop)
if [ ! -f /var/run/rsync.pid ];then
action "Rsync Service Stoppend...." /bin/false
else
rm -f /var/run/rsync.pid
pkill rsync
action "Rsync Stopting....." /bin/true
fi
;;
status)
if [ ! -f /var/run/rsync.pid ];then
echo "Rsync Service Status InActive...."
else
Rsync_Status=$(ps aux|grep rsync|grep -v grep|grep -v pts|awk '{print $2}')
echo "Rsync Service Status Active( "$Rsync_Status" )"
fi
;;
*)
echo "USAGE: $0 {start|stop|status}"
exit
esac
这个脚本有一个问题,问题就是脚本的名字里面不能包含rsync这样的关键字,在grep的会出问题,通过grep -v排除也不好用。
nginx
#!/usr/bin/bash
source /etc/init.d/functions
#加锁
Lock=/tmp/nginx.lock
if [ -f $Lock ];then
echo "此脚本正在运行,请稍后....."
exit
fi
touch $Lock
rc=$1
case $rc in
start)
if [ -f /var/run/nginx.pid ];then
sleep 10
action "nginx服务已经启动...." /bin/false
else
/usr/sbin/nginx
action "nginx服务启动成功..." /bin/true
fi
;;
stop)
if [ -f /var/run/nginx.pid ];then
/usr/sbin/nginx -s stop
if [ $? -eq 0 ];then
action "nginx关闭成功..." /bin/true
else
action "nginx关闭失败..." /bin/false
fi
else
action "nginx已经关闭...[error] open() /run/nginx.pid" /bin/false
fi
;;
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 "nginx重载成功..." /bin/true
else
action "nginx重载失败..." /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)
/usr/sbin/nginx -t
read -p "$nginx_conf 配置文件有错,在第 $nginx_line 行, 是否要需要进行配置修改 [y|n]: " re
case $re in
y|yes|YES)
vim +${nginx_line} ${nginx_conf}
;;
n|no|NO)
echo "你可以选择手动修改,再见!"
;;
*)
echo "USAGE: $0 {y|n} "
esac
fi
else
action "nginx没有启动,无法完成重载" /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 | reload | status ] "
esac
#解锁
rm -f $Lock
系统工具箱
cat <<-EOF
===================
h 显示命令帮助
f 显示磁盘分区
d 显示磁盘挂载
m 查看内存使用
u 查看系统负载
q 退出程序
=====================
EOF
read -p "请输入你需要做的操作: " sys
case $sys in
f)
df -h
;;
d)
mount -a |less
;;
u)
w
;;
q)
exit
;;
*)
echo "USAGE: $0 [ h | f | d | m | u | q ]"
esac
跳板机
引脚本需要提前将自己的root的公钥推送到其实主机root上去。
#!/usr/bin/bash
meminfo(){
cat <<-EOF
-------------------------------
| 1) db01-172.16.1.52 |
| 2) db02-172.16.1.53 |
| 3) db02-172.16.1.54 |
| h) help |
---------------------------------
EOF
}
# 调用函数打印菜单
meminfo
# 控制不让输入ctrl+c,z
trap "" HUP INT TSTP
while true
do
read -p "请输入你需要连接的主机序号[1|2|3|..]: " connection
case $connection in
1)
ssh root@172.16.1.52
;;
2)
ssh root@172.16.1.53
;;
3)
ssh root@172.16.1.54
;;
h)
clear
meminfo
;;
# 加一个后门
exec)
exit
;;
*)
echo "USAGE: $0 输入连接的主机编号即可 [ 1 | 2 | 3 | ]"
esac
done
trap "" HUP INT TSTP 的意思防止他按ctrl信号退出,但是存在一个问题,断开链接就退出去了,怎么办呢?把执行命令放到普通用户的家目录里面的./bashrc里面。
多级菜单
#!/usr/bin/bash
mem_option (){
cat <<-EOF
---------主菜单----------
| 1) 安装nginx |
| 2) 安装php |
| 3) 退出 |
--------------------------
EOF
}
mem_install_nginx(){
cat <<-EOF
-----Installed Nginx -----
| 1) 安装nginx1.1 |
| 2) 安装nginx1.2 |
| 3) 安装nginx1.3 |
| 4) 返回上一页 |
--------------------------
EOF
}
mem_install_php(){
cat <<-EOF
--------------------------
| 1) 安装php5.5 |
| 2) 安装php5.6 |
| 3) 安装php7.0 |
| 4) 返回上一页 |
--------------------------
EOF
}
#----------------------------------------------------------------------
while true
do
mem_option
read -p "请输入主菜单需要选择的选项,使用方法[1|2|3]: " option
case $option in
1)
while true
do
clear
mem_install_nginx
read -p "请输入你要安装Nginx的Version: " nginx_install_option
case $nginx_install_option in
1)
clear
echo "Installed Nginx Version 1.1 is Done....."
sleep 1
;;
2)
clear
echo "Installed Nginx Version 1.2 is Done....."
sleep 1
;;
3)
clear
echo "Installed Nginx Version 1.3 is Done....."
sleep 1
;;
4)
clear
break
;;
esac
done
;;
2)
clear
mem_install_php
read -p "请输入你要选择的php Version: " php_install_option
case $php_install_option in
1)
echo "Installed php Version 5.5 is Done....."
;;
2)
echo "Installed php Version 5.6 is Done....."
;;
3)
echo "Installed php Version 5.7 is Done....."
;;
4)
clear
;;
*)
esac
;;
3)
exit
;;
*)
echo "USAGE: [ 1 | 2 |3 ]"
esac
done