case语句,循环语句
vim 名字也不能乱写 比如
vim rsync.sh
应为后面需要pkill rsync 会把文件一起删掉
1.case流程控制语句
case 变量名4 in 模式匹配1) 命令的集合 ;; 模式匹配2) 命令的集合 ;; 模式匹配3) 命令的集合 ;; *) *的下一行不需要有;; echo USAGE[$0 1|2|3] esac
[root@shell /server/scripts]# cat case.sh #!/bin/bash ############################################################## # File Name: case.sh # Time: 2019-11-04-09:43:18 # Author: msy ############################################################## case $1 in Linux) echo linux... ;; Shell) echo shell... ;; MySql) echo mysql... ;; *) echo "USAGE $0 [Linux|Shell|MySql]" esac [root@shell /server/scripts]# sh case.sh Linux linux...
case 批量删除用户
[root@shell /server/scripts]# cat casedel.sh #!/bin/bash ############################################################## # File Name: casedel.sh # Time: 2019-11-04-10:22:07 # Author: msy ############################################################## read -p "请输入用户前缀名:" prefix read -p "请输入要删除用户数量:" num for i in `seq $num` do echo $prefix$i done read -p "你确定要删除以下用户吗?[y|Y|n|N]" ready for n in `seq $num` do name=$prefix$n case $ready in y|Y) id $name &>/dev/null if [ $? -eq 0 ];then userdel -r $name [ $? -eq 0 ] && echo "$name del is ok" else echo "id :$name: no such user" fi ;; n|N) echo "不删除我,你按什么删除命令?" && exit ;; *) echo "USAGE $0 [y|Y|n|N]" esac done
case菜单
[root@web scripts]# cat menu.sh #!/bin/sh echo -e "\t\t#########################" echo -e "\t\t#\t1.系统负载\t#" echo -e "\t\t#\t2.磁盘使用率\t#" echo -e "\t\t#\t3.内存使用率\t#" echo -e "\t\t#\t4.当前登录用户\t#" echo -e "\t\t#\t5.当前eth0的IP\t#" echo -e "\t\t#\t6.显示菜单\t#" echo -e "\t\t#########################" menu(){ cat<<EOF 1.u 系统负载 2.d 磁盘使用率 3.f 内存使用率 4.w 当前登录用户 5.ip 当前eth0的IP 6.h 显示帮助(菜单) 7.q 退出 EOF } menu while true do read -p "请输入你想要查看的系统信息的编号: " num case $num in 1|u) uptime ;; 2|d) df -h ;; 3|f) free -h ;; 4|w) w ;; 5|ip) ip add ;; 6|h) clear menu ;; 7|q) exit ;; *) menu esac done
[root@shell /server/scripts]# cat caidan.sh #!/bin/bash menu(){ cat<<EOF 0.菜单信息 1.系统版本 2.系统虚拟平台 3.系统内核版本 4.Eth0-IP地址 5.Eth1-IP地址 6.外网IP地址 7.内存使用情况 8.磁盘使用情况 9.退出 EOF } menu while true do read -p "请输入要查看的信息编号:" num case $num in 0) menu ;; 1) hostnamectl |awk 'NR==7{print $3,$4,$5,$6}' ;; 2) hostnamectl |awk 'NR==6{print $2}' ;; 3) hostnamectl |awk 'NR==9{print $2,$3}' ;; 4) ifconfig eth0 | awk 'NR==2{print $2}' ;; 5) ifconfig eth1 | awk 'NR==2{print $2}' ;; 6) curl -s http://icanhazip.com |awk 'NR==1{print $NF}' ;; 7) free -h ;; 8) df -h ;; 9) exit ;; *) echo "Usage:让你输入编号,你胡写啥呢?" esac done
案例
Nginx 启动脚本
jumpserver跳板机
/usr/sbin/nginx 使用命令进行启动后 systemctl 无法管理命令行启动的Nginx /usr/sbin/nginx 启动 /usr/sbin/nginx -s stop 停止 /usr/sbin/nginx -s reload 重新加载 平滑重启 /usr/sbin/nginx restart 重启 当ip地址改变时,必须重启才生效 sleep 2 /usr/sbin/nginx 自取状态 PID 端口 状态 [root@web scripts]# sh nginxstart.sh status 当前Nginx的PID:116528 [root@web scripts]# cat nginxstart.sh #!/bin/sh . /etc/init.d/functions en=$1 fun(){ [ $? -eq 0 ] && action "Nginx $en is" /bin/true || action "Nginx $en is" /bin/false } case $1 in start) /usr/sbin/nginx fun ;; stop) /usr/sbin/nginx -s stop fun ;; reload) /usr/sbin/nginx -s reload fun ;; restart) /usr/sbin/nginx -s stop sleep 2 /usr/sbin/nginx fun ;; status) echo "当前Nginx的PID:`ps axu|grep nginx|grep master|awk '{print $2}'`" ;; *) echo "USAGE $0 [start|stop|reload|restart|status]" esac [root@shell /server/scripts]# cat nginx.sh #!/bin/bash ############################################################## # File Name: nginx.sh # Time: 2019-11-05-17:18:53 # Author: msy ############################################################## [ -f /etc/init.d/functions ] && source /etc/init.d/functions if [ ! $USER == "root" -a ! $UID -eq 0 ];then echo "该用户$USER 对此脚本$0 没有执行权限" exit fi if [ $# -ne 1 ];then echo "请输入一个参数" echo "USAGE $0 [start|stop|reload|restart|status]" exit fi Pid=/var/run/nginx.pid case $1 in start) if [ -f $Pid ];then action "Nginx服务正在运行" /bin/true else /usr/sbin/nginx &>/dev/null if [ $? -eq 0 ];then action "Nginx服务启动成功" /bin/true else action "Nginx服务启动失败" /bin/false exit fi fi ;; stop) if [ -f $Pid ];then /usr/sbin/nginx -s stop &>/dev/null if [ $? -eq 0 ];then action "Nginx服务停止成功" /bin/true else action "Nginx服务停止失败" /bin/false fi else action "Nginx服务没有运行" /bin/true fi ;; reload) if [ -f $Pid ];then /usr/sbin/nginx -s reload &>/dev/null if [ $? -eq 0 ];then action "Nginx服务重载成功" /bin/true else action "Nginx服务重载失败" /bin/false fi else action "Nginx服务没有运行,无法进行重载" /bin/false fi ;; status) if [ -f $Pid ];then action "Nginx服务正在运行" /bin/true else action "Nginx服务没有运行" /bin/true fi ;; restart) if [ -f $Pid ];then /usr/sbin/nginx -s stop &>/dev/null if [ $? -eq 0 ];then action "Nginx服务停止成功" /bin/true else action "Nginx服务重载失败" /bin/false fi sleep 3 /usr/sbin/nginx &>/dev/null if [ $? -eq 0 ];then action "Nginx服务重启成功" /bin/true else action "Nginx服务重启失败" /bin/false fi else ▽ action "Nginx服务没有运行" /bin/true /usr/sbin/nginx &>/dev/null if [ $? -eq 0 ];then action "Nginx服务启动成功" /bin/true else action "Nginx服务启动失败" /bin/false fi fi ;; *) echo "USAGE $0 [start|stop|reload|restart|status]" esac
跳板机
[root@shell /server/scripts]# cat jump.sh #!/bin/bash ############################################################## # File Name: jump.sh # Time: 2019-11-06-15:12:04 # Author: msy ############################################################## WEB01=10.0.0.7 WEB02=10.0.0.8 MySQL=10.0.0.51 NFS=10.0.0.31 menu(){ cat<<EOF 1. WEB01=10.0.0.7 2. WEB02=10.0.0.8 3. MySQL=10.0.0.51 4. NFS=10.0.0.31 5. 显示菜单 6. 退出 EOF } menu trap "echo 来了老弟?还想走?" HUP INT TSTP 禁止终止命令 while true do read -p "请输入要连接的主机:" num case $num in 1) ssh root@$WEB01 ;; 2) ssh root@$WEB02 ;; 3) ssh root@$MySQL ;; 4) ssh root@$NFS ;; 5) clear menu ;; 6) echo "霍!你别想退出" ;; woshiyunwei) exit esac done
安装服务脚本
[root@shell /server/scripts]# cat qidongmenu.sh #!/bin/bash ############################################################## # File Name: qidongmenu.sh # Time: 2019-11-06-16:18:21 # Author: msy ############################################################## master_menu(){ cat<<EOF ###################### 1.安装Nginx 2.安装PHP 3.退出 ###################### EOF } Nginx_menu(){ cat<<EOF ###################### 1.安装Nginx-1.12 2.安装Nginx-1.14 3.安装Nginx-1.16 4.返回主菜单 ###################### EOF } PHP_menu(){ cat<<EOF ###################### 1.安装PHP-5.5 2.安装PHP-5.7 3.安装PHP-7.1 4.返回主菜单 ###################### EOF } master_menu trap "echo 老弟进来了还想跑?" HUP INT TSTP 禁止终止 while true do read -p "请输入你要安装的服务:" Server case $Server in 1) clear 清屏 Nginx_menu while true do read -p "请输入要安装的版本:" Version case $Version in 1) echo "你选择是安装Nginx-1.12版本....." echo "正在安装....." sleep 2 echo "Nginx-1.12版本安装完成" ;; 2) echo "你选择是安装Nginx-1.14版本....." echo "正在安装....." sleep 2 echo "Nginx-1.14版本安装完成" ;; 3) echo "你选择是安装Nginx-1.16版本....." echo "正在安装....." sleep 2 echo "Nginx-1.16版本安装完成" ;; 4) clear master_menu break 跳出循环,继续向下执行 esac done ;; 2) clear PHP_menu while true do read -p "请输入你要安装的PHP版本:" install case $install in 1) echo "你选择是安装PHP-5.5版本....." echo "正在安装......." sleep 2 echo "安装PHP-5.5版本完成....." ;; 2) echo "你选择是安装PHP-5.6版本....." echo "正在安装......." sleep 2 echo "安装PHP-5.6版本完成....." ;; 3) echo "你选择是安装PHP-7.1版本....." echo "正在安装......." sleep 2 echo "安装PHP-7.1版本完成....." ;; 4) clear master_menu break esac done ;; 3) echo "进来容易,出去难" ;; woshiyunwei) exit ;; *) echo "请按照要求输入" esac done
2.for循环
IFS=: 以:为分隔符
IFS=': ' 指定多个分隔符
for 变量名 in [取值列表] 苹果 香蕉 梨 桃子 西瓜 字符串 数字{}序列 命令 do 命令体 echo 呵呵 done [root@shell /server/scripts]# vim 1.sh #!/bin/bash read -p "请输入创建用户前缀:" name read -p "请输入创建用户数量:" num read -p "确认创建$name$num? Y|y|N|n:" qr if [ $qr == "Y|y" ];then echo "创建失败" exit fi for i in `seq $num` do echo $name$i done [root@shell /server/scripts]# sh 1.sh 请输入创建用户前缀:m 请输入创建用户数量:5 确认创建m5? Y|y|N|n:y m1 m2 m3 m4 m5
使用for循环创建用户
cat user.txt zs ls em oldboy [root@web scripts]# cat for1.sh #!/bin/sh for i in `cat user.txt` do useradd $i done
命令行中使用for循环批量删除用户
for i in `cat user.txt`;do userdel -r $i;done
3.while循环
#模式一 a=1 while [ $a -lt 10 ] 条件判断方式 do done #模式二 while read line 读入文件方式 do done < test.txt #模式三 while true 条件为真 一直循环 do done
两数相乘
#!/bin/bash a=1 b=10 while [ $b -gt 0 ] do echo "$a * $b = $(( $a * $b ))" let a++ let b-- done
在/test下创建8天文件,文件名带时间
time=1 [ -d /test/ ] || mkdir -p /test/ while [ $time -le 8 ] do date -s "2019110$time" &>/dev/null if [ $? -eq 0 ];then touch /test/`date +%F`.txt let time++ if [ $? -eq 0 ];then echo "文件创建成功" else echo "文件创建失败" fi else echo "时间修改失败" fi done [root@shell /test]# ll total 0 -rw-r--r-- 1 root root 0 Nov 1 00:00 2019-11-01.txt -rw-r--r-- 1 root root 0 Nov 2 00:00 2019-11-02.txt -rw-r--r-- 1 root root 0 Nov 3 00:00 2019-11-03.txt -rw-r--r-- 1 root root 0 Nov 4 00:00 2019-11-04.txt -rw-r--r-- 1 root root 0 Nov 5 00:00 2019-11-05.txt -rw-r--r-- 1 root root 0 Nov 6 00:00 2019-11-06.txt -rw-r--r-- 1 root root 0 Nov 7 00:00 2019-11-07.txt -rw-r--r-- 1 root root 0 Nov 8 00:00 2019-11-08.txt
while read
`判断一个文件中总共的行数 `统计行号 [root@web scripts]# cat while4.sh #!/bin/sh while read line do let i++ done</etc/passwd echo $i
while read line line 变量名 按行读取文件的内容 for循环是按照空格分隔读取文件内容
while read line do if id $line &>/dev/null;then echo "用户已经存在" continue fi if useradd $line &>/dev/null;then echo "用户创建成功" else echo "用户创建失败" fi done < user.txt
while read line do User=`echo $line |awk -F: '{print $1}'` Pass=`echo $line |awk -F: '{print $2}'` id $User &>/dev/null if [ $? -eq 0 ];then echo "$User 用户已经存在" else useradd $User &>/dev/null && echo $Pass|passwd --stdin $User &>/dev/null if [ $? -eq 0 ];then echo "用户创建成功" else echo "用户创建失败" fi fi done < user.txt
until 循环
until 循环执行一系列命令直至条件为 true 时停止。
until 循环与 while 循环在处理方式上刚好相反。
一般 while 循环优于 until 循环,但在某些时候—也只是极少数情况下,until 循环更加有用。
until 语法格式:
until condition do command done
4.流量控制语句
exit 退出整个脚本 不会继续执行
break 跳出本次循环 继续往下执行 跳出循环体
continue 结束当前此次的命令,继续下一次循环,不执行下面的命令了
[root@shell /server/scripts]# vim ll.sh #!/bin/bash ######################################################## ###### # File Name: ll.sh # Time: 2019-11-04-14:44:55 # Author: msy ######################################################## ###### while true do echo "ok" exit echo hehe done echo "done..." [root@shell /server/scripts]# sh ll.sh ok
[root@shell /server/scripts]# vim lll.sh #!/bin/bash ######################################################## ###### # File Name: lll.sh # Time: 2019-11-04-14:44:55 # Author: msy ######################################################## ###### while true do echo "ok" break echo hehe done echo "done..." [root@shell /server/scripts]# sh lll.sh ok done...
[root@shell /server/scripts]# vim lll.sh #!/bin/bash ######################################################## ###### # File Name: llll.sh # Time: 2019-11-04-14:44:55 # Author: msy ######################################################## ###### #!/bin/sh while true do echo ok continue echo hehe done echo done...... [root@shell /server/scripts]# sh llll.sh ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok
创建用户
(for i in {1..10};do userdel -r oldboy$i;done)命令行测试删除
exit
[root@shell /server/scripts]# cat useradd.sh #!/bin/bash ############################################################## # File Name: useradd.sh # Time: 2019-11-01-10:02:52 # Author: msy ############################################################## for i in `seq 10` do user=oldboy$i id $user &>/dev/null if [ $? -ne 0 ];then useradd $user [ $? -eq 0 ] && echo "$user create is ok" else exit 直接退出 fi done echo "hehe..." [root@shell /server/scripts]# sh useradd.sh 之前已经有oldboy5用户了 oldboy1 create is ok oldboy2 create is ok oldboy3 create is ok oldboy4 create is ok
break
[root@shell /server/scripts]# cat useradd.sh #!/bin/bash ############################################################## # File Name: useradd.sh # Time: 2019-11-01-10:02:52 # Author: msy ############################################################## for i in `seq 10` do user=oldboy$i id $user &>/dev/null if [ $? -ne 0 ];then useradd $user [ $? -eq 0 ] && echo "$user create is ok" else break 跳出循环 到下一个命令中去 fi done echo "hehe..." [root@shell /server/scripts]# sh useradd.sh oldboy1 create is ok oldboy2 create is ok oldboy3 create is ok oldboy4 create is ok hehe...
continue
[root@shell /server/scripts]# cat useradd.sh #!/bin/bash ############################################################## # File Name: useradd.sh # Time: 2019-11-01-10:02:52 # Author: msy ############################################################## for i in `seq 10` do user=oldboy$i id $user &>/dev/null if [ $? -ne 0 ];then useradd $user [ $? -eq 0 ] && echo "$user create is ok" else continue 忽略你 结束当前命令 继续循环命令 就是忽略这一条命令了 fi done echo "hehe..." [root@shell /server/scripts]# sh useradd.sh oldboy1 create is ok oldboy2 create is ok oldboy3 create is ok oldboy4 create is ok oldboy6 create is ok oldboy7 create is ok oldboy8 create is ok oldboy9 create is ok oldboy10 create is ok hehe...
来源:https://www.cnblogs.com/MENGSHIYU/p/11884985.html