?????博主簡介
????云計(jì)算領(lǐng)域優(yōu)質(zhì)創(chuàng)作者
????華為云開發(fā)者社區(qū)專家博主
????阿里云開發(fā)者社區(qū)專家博主
??交流社區(qū):運(yùn)維交流社區(qū) 歡迎大家的加入!
?? 希望大家多多支持,我們一起進(jìn)步!??
??如果文章對你有幫助的話,歡迎 點(diǎn)贊 ???? 評論 ?? 收藏 ?? 加關(guān)注+??
?? if 判斷
?? if 判斷格式:
#單條件判斷
if [ 條件測試 ];then
執(zhí)行代碼
fi
if [ 條件測試 ];then
執(zhí)行代碼
eles
執(zhí)行代碼
fi
#多條件判斷
if [ 條件測試 ];then
執(zhí)行代碼
elif [ 條件測試 ];then
執(zhí)行代碼
eles
執(zhí)行代碼
fi
#單行if判斷
if [ 條件測試 ]; then 執(zhí)行代碼; else 執(zhí)行代碼; fi
?? if 判斷實(shí)例:
?? 單條件判斷實(shí)例:判斷是否為整數(shù)
#!/bin/bash
read -p "please input a number:" num
expr 10 + $num &>/dev/null
if [ $? -eq 0 ];then
echo "${num}是整數(shù)"
else
echo "${num}不是整數(shù)"
fi
?? 多條件判斷實(shí)例:成績及格否
對輸入成績做判斷:
如果成績小于60;那么
打印不及格
如果60>=成績<80;那么
打印及格
如果80>=成績<90;那么
打印良好
如果90>=成績<=100;那么
打印優(yōu)秀
否則
請輸入0-100的整數(shù)
結(jié)尾
#!/bin/bash
read -p "please input is results:" results
expr 10 + $results &>/dev/null
if [ $? -ne 0 ];then
echo "${results}不是合法整數(shù)"
exit 1
fi
if [ ${results} -lt 60 ];then
echo "不及格"
elif [ ${results} -ge 60 ] && [ ${results} -lt 80 ];then
echo "及格"
elif [ ${results} -ge 80 ] && [ ${results} -lt 90 ];then
echo "良好"
elif [ ${results} -ge 90 ] && [ ${results} -le 100 ];then
echo "優(yōu)秀"
else
echo "請輸入0-100之間合法的整數(shù),您輸入的是:${results}"
fi
?? 單行if判斷實(shí)例:判斷是否有這個(gè)文件或目錄
if [ ! -d "/var/lib/mysql/" ]; then echo "目錄不存在"; else echo "目錄存在"; fi
?? 單行if判斷實(shí)例:判斷這個(gè)目錄中是否存在文件(前提是有這個(gè)目錄)
if [ -z "$(ls -A /var/lib/mysql)" ]; then echo "該目錄不存在文件"; else echo "該目錄存在文件"; fi
?? for 循環(huán)
?? for 循環(huán)格式:
for 變量 in 取值列表;do
執(zhí)行代碼
done
for 變量 in 取值列表
do
執(zhí)行代碼
done
for (( i=1;i<20;i++ ));do
執(zhí)行代碼;(表示循環(huán)20次執(zhí)行代碼)
done
取值列表中的值都將被歷遍
?? for 循環(huán)實(shí)例:
?? 需求1:打印10以內(nèi)的偶數(shù)–>能被2整除,除2余數(shù)為0
#!/bin/bash
#{起始數(shù)字..終止數(shù)字..步長} 默認(rèn)步長為1
for i in {2..10..2};do
echo $i
done
continue 跳過當(dāng)前循環(huán)
break 終止當(dāng)前循環(huán)
exit 退出腳本,同時(shí)可以指定退出時(shí)的狀態(tài)碼
seq [起始位置(不指定默認(rèn)是1)] 終止位置
-s 指定分隔符
-w 補(bǔ)齊相同寬度
?? 需求2:循環(huán)創(chuàng)建文件cs{1…100}
#!/bin/bash
#獲取腳本執(zhí)行的路徑
DIR=$(cd $(dirname $0) && pwd )
text=$DIR/for
[ ! -d $text ] && mkdir $text
for I in {1..100};do
touch ${text}/cs$I
done
#其實(shí)直接用touch也可以創(chuàng)建,這里只為了演示
#touch cs{1..100}
#會在當(dāng)前目錄創(chuàng)建cs{1..100}文件
?? 需求3:從變量中取遍歷的值
#!/bin/bash
List="file1 file2 file3"
for I in $List;do
echo "當(dāng)前文件為 ${I}"
done
?? 需求4:從命令中取值
#!/bin/bash
for I in $(cat /etc/passwd)
do
echo "$I"
done
?? 需求5:for循環(huán)自增自減
?? 需求5.1:批量創(chuàng)建cs1-cs10用戶
#!/bin/bash
for (( i=1;i<=10;i++ ));do
useradd cs_$i
done
如果需要刪除這些用戶,可在腳本中把useradd
改為userdel -r
即可;
?? 需求5.2:輸出數(shù)字 a 自增、 a自增、 a自增、b自減
#!/bin/bash
for (( a=1,b=9;a<=10;a++,b-- ));do
echo "num is $a $b"
done
a的初始值為1;b的初始值為9;
a在前,所以寫a最大不能>10;
a每次加1,直至加到10;b每次-1,直至a停止。
?? 需求5.3:循環(huán)20次zabbix調(diào)用
#!/bin/bash
for (( i=1;i<=20;i++ ));do
cs=$(zabbix_get -s 172.16.10.1 -k cs)
echo "調(diào)用$i次!調(diào)用結(jié)果為:$cs"
echo "------------------------------------------------------------------------"
done
?? 需求6:批量創(chuàng)建用戶,密碼默認(rèn)為123456
#!/bin/bash
#獲取腳本執(zhí)行的路徑
DIR=$(cd $(dirname $0) && pwd )
#創(chuàng)建一個(gè)用戶組
groupadd mailgroup 2>/dev/null
for i in $(cat $DIR/a.txt);do
useradd -g mailgroup -s /sbin/nologin $i 2>/dev/null
echo 123456 | passwd --stdin $i
done
a.txt 中名字可以隨便起;
?? while 循環(huán)
?? while 循環(huán)格式:
while [條件測試];do
執(zhí)行代碼
done
while [條件測試]
do
執(zhí)行代碼
done
while true;do
執(zhí)行代碼(無限循環(huán)此命令,每次循環(huán)停留兩秒)
sleep 2
done
條件測試比如:$i -le 5
當(dāng)條件成立時(shí),執(zhí)行循環(huán),不成立,結(jié)束循環(huán)。
如果while中的條件永遠(yuǎn)成立,會一直循環(huán),成為死循環(huán)。
?? while 循環(huán)實(shí)例:
?? 需求1:計(jì)算1到100正整數(shù)的和
#!/bin/bash
declare -i I=1
declare -i SUM=0
while [ $I -le 100 ]; do
let SUM+=$I
let I++
done
echo "$SUM"
在執(zhí)行腳本時(shí)加個(gè) -x
可查看執(zhí)行過程
?? 需求2:批量創(chuàng)建用戶,密碼默認(rèn)為123456
#!/bin/bash
#獲取腳本執(zhí)行的路徑
DIR=$(cd $(dirname $0) && pwd )
#創(chuàng)建一個(gè)用戶組
groupadd mailgroup 2>/dev/null
while read username; do
useradd -g mailgroup -s /sbin/nologin $username 2>/dev/null
echo 123456 | passwd --stdin $username
done < $DIR/username.txt
username.txt
lcy
cs
abc
abb
abd
abe
abf
批量刪除用戶
#!/bin/bash
#獲取腳本執(zhí)行的路徑
DIR=$(cd $(dirname $0) && pwd )
#創(chuàng)建一個(gè)用戶組
groupadd mailgroup 2>/dev/null
cat $DIR/username.txt | while read username; do
userdel -r $username
done
?? 需求3:提示用戶輸入字符,如果是小寫就把字母全部變?yōu)榇髮懀绻莙uit則退出腳本
#!/bin/bash
#如果輸入的不是quit則把小寫字母全部換成大寫字母,如果輸入的是quit則退出循環(huán),否則會一直循環(huán);
read -p "Please enter content:" SCRIPT
while [ $SCRIPT != "quit" ]; do
echo "$SCRIPT" | tr 'a-z' 'A-Z'
read -p "Please enter content:" SCRIPT
done
字母寫的小寫,全部會替換為大寫,數(shù)字和其他字符不會被替換;
?? 需求4:無限循環(huán)執(zhí)行zabbix_get調(diào)用命令
#!/bin/bash
while true;do
cs=$(zabbix_get -s 172.16.10.1 -k cs)
echo "結(jié)果為$cs"
echo "----------------------------------------"
sleep 2
done
?? case 循環(huán)判斷
?? case 循環(huán)判斷格式:
case 變量 in
條件或值1)
執(zhí)行代碼
;;
條件或值2)
執(zhí)行代碼
;;
條件或值3)
執(zhí)行代碼
;;
*)
執(zhí)行代碼
esac
?? case 循環(huán)判斷實(shí)例:
?? nginx服務(wù)啟停腳本:函數(shù) + case
#!/bin/bash
ngstart (){
/usr/local/nginx/sbin/nginx
}
ngstop (){
/usr/local/nginx/sbin/nginx -s stop
}
ngrestart (){
/usr/local/nginx/sbin/nginx -s reload
}
ngstatus (){
/usr/local/nginx/sbin/nginx -s status
}
#nginx服務(wù)器起停的腳本
case $1 in
start)
echo "啟動nginx服務(wù)"
ngstart
;;
stop)
echo "關(guān)閉nginx服務(wù)"
ngstop
;;
restart)
echo "重啟nginx服務(wù)"
# $0 stop
# $0 start
ngrestart
;;
status)
echo "查看nginx服務(wù)狀態(tài)"
ngstatus
;;
*)
echo "USage: /etc/init.d/nginx {start|stop|restart}"
esac
文章來源:http://www.zghlxwxcb.cn/news/detail-649609.html
推薦一個(gè)使用case寫的啟動腳本,包含啟動,停止及重啟監(jiān)測狀態(tài):linux啟動、關(guān)閉、重啟jar包shell腳本 文章來源地址http://www.zghlxwxcb.cn/news/detail-649609.html
相關(guān)文章:
文章名 | 文章地址 |
---|---|
【Linux】 shell腳本的創(chuàng)建及使用 《入門到實(shí)踐》詳解[?建議收藏??!?] | https://liucy.blog.csdn.net/article/details/130111812 |
【Linux】Shell腳本之函數(shù)的操作+實(shí)戰(zhàn)詳解[?建議收藏?。?] | https://liucy.blog.csdn.net/article/details/130387377 |
【Linux】Shell腳本之流程控制語句 if判斷、for循環(huán)、while循環(huán)、case循環(huán)判斷 + 實(shí)戰(zhàn)詳解[?建議收藏!!?] | https://liucy.blog.csdn.net/article/details/130387523 |
到了這里,關(guān)于【Linux】Shell腳本之流程控制語句 if判斷、for循環(huán)、while循環(huán)、case循環(huán)判斷 + 實(shí)戰(zhàn)詳解[?建議收藏??!?]的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!