shell指令碼中的4種迴圈語句使用
阿新 • • 發佈:2020-09-11
1、for迴圈
#語法結構
#第一種:取值變數
for 變數名 in 變數取值表 do
指令 done
#例子:
#示例 for a in {1..9} do mkdir dir$a done
#第二種:C語言型for迴圈
for ((exp1; exp2; exp3)) do 指令 done
#例子:
#示例 for ((i=1;i<=3;i++)) do echo $i done #解釋:i從1開始,當i<=3就可以執行,如果執行的值大於3,就退出迴圈 #語法結構講解 for關鍵字後的雙括號是三個表示式, 第一個是變數初始化(例如:i=1),第二個為變數的範圍(例如i<=3),第三個為變數自增或自減(例如i++)。 當第一個表示式的初始化值符合第二個變數的範圍時,就進行如迴圈執行,當條件不滿足時就退出迴圈
#簡單示例
#1.豎向列印10 9 87 6 5幾個數字
#第一種方法:直接列出元素
[root@game scripts]# cat for1.sh #!/bin/bash for i in 1 2 3 4 5 do echo $i done #效果 [root@game scripts]# sh for1.sh 1 2 3 4 5
第二種方法:使用大括號{}生成數字序列
[root@game scripts]# cat for2.sh #!/bin/bash for i in {1..5} do echo $i done #效果 [root@game scripts]# sh for2.sh 1 2 3 4 5
#第三種方法:使用seq生成數字序列
[root@game scripts]# cat for3.sh #!/bin/bash for i in `seq 1 5` do echo $i done #效果 [root@game scripts]# sh for3.sh 1 2 3 4 5
#2.獲取當前目錄下的目錄或檔名,並將其作為變數列表列印輸出
#資料 [root@game~]# mkdir -p /test/{test1.txt,test2.txt,guo.txt,ke.txt} [root@game ~]# ls -l /test/ total 0 drwxr-xr-x. 2 root root 6 Aug 21 22:14 guo.txt drwxr-xr-x. 2 root root 6 Aug 21 22:14 ke.txt drwxr-xr-x. 2 root root 6 Aug 21 22:14 test1.txt drwxr-xr-x. 2 root root 6 Aug 21 22:14 test2.txt # 編寫指令碼 [root@game scripts]# cat for4.sh #!/bin/bash usage(){ echo "directory not found" } [ ! -d /test ] && usage && exit 1 cd /test for i in `ls` do echo $i done # 效果 [root@game scripts]# sh for4.sh guo.txt ke.txt test1.txt test2.txt
2、while迴圈
#while迴圈一般應用於守護程序程式或一直迴圈執行
#語法格式
while <條件表示式> do 指令 done
#簡單示例
每隔2秒在螢幕上輸出一次負載值 [root@game scripts]# cat while1.sh #!/bin/bash while true do uptime sleep 2 #暫停2秒再執行 done #提示:while true表示條件永遠為真,因此會一直執行,像死迴圈一樣,稱為守護程序 #效果:每隔2秒就輸出一次 [root@game scripts]# sh while1.sh 23:11:35 up 2 days, 2:00, 2 users, load average: 0.00, 0.01, 0.05 23:11:37 up 2 days, 2:00, 2 users, load average: 0.00, 0.01, 0.05 23:11:39 up 2 days, 2:00, 2 users, load average: 0.00, 0.01, 0.05
3、until迴圈
#until迴圈是當條件表示式不成立時,就會進入迴圈,當條件表示式成立時,就會終止迴圈
#語法格式
until <條件表示式> do 指令 done
#示例
#如果使用者輸出的是guoke就符合條件,退出迴圈,如果不是,使用者輸入3次之後就退出迴圈 [root@game scripts]# cat until1.sh #!/bin/bash i=1 until [ "$user" = "guoke" -o "$i" -gt 3 ] do read -p "please enter you username:" user let i++ done #效果 [root@game scripts]# sh until1.sh please enter you username:guoke [root@game scripts]# sh until1.sh please enter you username:1 please enter you username:1 please enter you username:1 [root@game scripts]#
4、select迴圈
#語法格式
select 變數名 in [選單取值列表] do 指令 done
#示例
#第一種:直接使用列表字串 [root@game scripts]# cat select1.sh #!/bin/bash select name in apache httpd nginx tomcat do echo $name done #效果 [root@game scripts]# sh select1.sh 1) apache 2) httpd 3) nginx 4) tomcat #? 1 apache #? 3 nginx #? 4 tomcat #? ^C #第二種:採用陣列做變數列表 [root@game scripts]# cat select2.sh #!/bin/bash array=(apache nginx tomcat lighttpd) select name in "${array[@]}" do echo $name done #效果 [root@game scripts]# sh select2.sh 1) apache 2) nginx 3) tomcat 4) lighttpd #? 3 tomcat #? 4 lighttpd #? ^C
5.迴圈控制及狀態返回值
break (迴圈控制)
continue (迴圈控制)
exit (退出指令碼)
return (退出函式)
#區別
break continue在條件語句及迴圈語句(for if while等)中用於控制程式的走向 exit是終止所有語句並退出指令碼 return:僅用於在函式內部返回函式執行的狀態值
#break示例
#如果i等於3,那麼就終止迴圈 [root@game scripts]# cat break1.sh #!/bin/bash for ((i=0;i<=5;i++)) do if [ $i -eq 3 ];then break else echo $i fi done echo "1111" yum install net-tools -y > /dev/null [ $? -eq 0 ] && echo "already install" #效果 [root@game scripts]# sh break1.sh 0 1 2 1111 already install #說明:i等於3的時候就終止迴圈,但是沒有跳出指令碼
#exit示例
[root@game scripts]# cat exit1.sh #!/bin/bash for ((i=0;i<=5;i++)) do if [ $i -eq 3 ];then exit 1 fi echo $i done echo "ok" #執行效果 [root@game scripts]# sh exit1.sh 0 1 2 #說明:當i等於3的時候就會退出指令碼了,就不會執行後面的語句
#continue示例
[root@game scripts]# cat con1.sh #!/bin/bash for ((i=0;i<=5;i++)) do if [ $i -eq 3 ];then continue else echo $i fi done echo "ok" #執行效果 [root@game scripts]# sh con1.sh 0