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