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...