if-then-else是一種基於條件測試結果的流程控制結構。若是測試結果爲真,則執行控制結構中相應的命令列表;不然將進行另一個條件測試或者退出該控制結構。bash
if-then-else語法格式:測試
if 條件1 then 命令列表1 elif 條件2 then 命令列表2 else 命令列表3 fi
執行邏輯流程圖spa
說明:當條件1成立時,則執行命令列表1並退出if-then-else控制結構;若是條件2成立,則執行命令列表2並退出if-then-else控制結構;不然執行命令列表3並退出if-then-else控制結構。在同一個if-then-else結構中只能有一條if語句和一條else語句,eilf語句能夠有多條。其中if語句是必須的,elif和else語句是可選的。命令行
Shell腳本首先判斷文件test1是否可讀,若是是,則輸出 is readable !的提示信息;不然不進行任何動做。code
[root@localhost 20190105]# vi test.sh filename=test1 if [ -r $filename ] //輸出test1可讀則輸出信息 then echo $filename' is readable !' fi [root@localhost 20190105]# sh test.sh test1 is readable !
Shell腳本會判斷number變量是否等於100,若是是,則輸出 The number is equal 100 !的提示;不然輸出 The number is not equal 100 !。blog
[root@localhost 20190105]# vi number.sh number=200 if [ $number -eq 100 ] //若是number等於100則輸出「The number is equal 100 !」提示 then echo 'The number is equal 100 !' else //不然輸出「The number is not equal 100 !」提示 echo 'The number is not equal 100 !' fi [root@localhost 20190105]# sh number.sh The number is not equal 100 !
Shell腳本首先判斷number變量是否小於10,若是是則輸出 The number < 10 !;不然,判斷number變量是否大於等於10且小於20。若是是則輸出 10 =< The number < 20 !;不然,判斷 number變量是否大於等於20且小於30。若是是,則輸出 20 =< The number < 30 !;不然,輸出 30 <= The number !。ip
[root@localhost 20190105]# vi number1.sh number=25 if [ $number -lt 10 ] //若是number小於10 then echo 'The number < 10 !' elif [ $number -ge 10 -a $number -lt 20 ] //若是number大於等於10且小於20 then echo '10 =< The number < 20 !' elif [ $number -ge 20 -a $number -lt 30 ] //若是number大於等於20且小於30 then echo '20 =< The number < 30 !' else //除上述3種狀況之外的其餘狀況 echo '30 <= The number !' fi [root@localhost 20190105]# sh number1.sh 20 =< The number < 30 !
if-then-else結構可以支持多路的分支(多個elif語句),但若是有多個分支,那麼程序就會變得難以閱讀。case結構提供了實現多路分支的一種更簡潔的方法。input
case語法格式:it
case 值或變量 in 模式1) 命令列表1 ;; 模式2) 命令列表2 ;; ... esac
case語句後是須要進行測試的值或者變量。Shell會順序地把須要測試的值或變量與case結構中指定的模式逐一進行比較,當匹配成功時,則執行該模式相應的命令列表並退出case結構(每一個命令列表以兩個分號「;;」做爲結束)。若是沒有發現匹配的模式,則會在esac後退出case結構。for循環
以下該腳本對number變量的值進行測試,與模式匹配的話,則輸出相應的信息。
[root@localhost 20190105]# vi case.sh number=66 case $number in 33) echo 'The number is 33 !' //number 變量等於 33 ;; 44) echo 'The number is 44 !' //number 變量等於 44 ;; 55) echo 'The number is 55 !' //number 變量等於 55 ;; 66) echo 'The number is 66 !' //number 變量等於 66 ;; 77) echo 'The number is 77 !' //number 變量等於 77 ;; 88) echo 'The number is 88 !' //number 變量等於 88 ;; esac //結束 case 結構 [root@localhost 20190105]# sh case.sh The number is 66 ! //命令的輸出結果
for循環結構能夠重複執行一個命令列表,基於for語句中所指定的值列表決定是繼續循環仍是跳出循環。for循環在執行命令列表前會先檢查值列表中是否還有未被使用的值,若有的話,則把該值賦給for語句中指定的變量,而後執行循環結構中的命令列表。如此循環,直到值列表中的全部值都被使用。
for循環結構語法:
for 變量名 in 值列表 do 命令1 命令2 命令3 ... done
以常量做爲值列表
使用變量一、二、三、四、五、6做爲值列表,for循環中只是簡單的把值列表中的值進行輸出。
[root@localhost 20190105]# vi for1.sh #!/bin/bash for n in 1 2 3 4 5 6 //循環讀取 1-6 do echo $n done 由運行結果能夠很是清楚的瞭解for循環的運行過程。 [root@localhost 20190105]# sh for1.sh 1 2 3 4 5 6
以變量做爲值列表
值列表能夠是一個環境變量。
[root@localhost 20190105]# vi for2.sh #!/bin/bash values="1 2 3 4 5 6" //對 values 變量賦值 for n in $values //循環讀取 values 變量中的值 do echo $n done [root@localhost 20190105]# sh for2.sh 1 2 3 4 5 6
以命令運行結果做爲值列表
Shell支持使用命令的運行結果做爲for循環的值列表。在Shell中經過"`命令`"或者「$(命令)」來引用命令的運行結果。將會以ls命令的結果做爲值列表。
[root@localhost 20190105]# vi for3.sh #!/bin/bash for n in `ls` //循環讀取 ls 命令的輸出結果 do echo $n //輸出變量 n 的值 done [root@localhost 20190105]# sh for3.sh case.sh for1.sh for2.sh for3.sh HelloWorld.sh number1.sh number.sh test1 test2 test.sh
expr是一個命令行的計數器,用於加、減、乘、除運算。
[root@localhost 20190105]# expr 123 + 456 - 78 //123 加 456 減 78 等於 501 501 [root@localhost 20190105]# expr 9 \* 8 //9 乘以 8 等於 72 72 [root@localhost 20190105]# expr 666 / 8 // 666 除以 8 等於 83 83
在循環結構中,expr 會被用做增量計算,初始值爲10,每次使用expr增長加11/12。注意:這裏使用expr命令時都使用的是反撇號,不是單引號。
[root@localhost 20190105]# number=10 [root@localhost 20190105]# number=`expr $number + 11` //對number變量的值加11 [root@localhost 20190105]# echo $number 21 [root@localhost 20190105]# number=`expr $number + 12` //對number變量的值加12 [root@localhost 20190105]# echo $number 33
while結構會循環執行一系列的命令,並基於while語句中所指定的測試條件決定是繼續循環仍是跳出循環。若是條件爲真,則while循環會執行結構中的一系列命令。命令執行完畢後,控制返回循環頂部,從頭開始從新執行直到測試條件爲假。
while循環結構的語法:
while 條件 do 命令1 命令2 ... done
循環增量計算:是在while循環中使用增量計算,其運行結果以下。
[root@localhost 20190105]# vi while1.sh #!/bin/bash count=0 //將 count 變量置 0 #當 count 變量小於5時繼續循環 while [ $count -lt 5 ] do #每循環一次,count 變量的值加1 count=`expr $count + 1` echo $count done [root@localhost 20190105]# sh while1.sh 1 2 3 4 5 [root@localhost 20190105]#
循環從文件中讀取內容
現有一文件,保存了學生的成績信息,其中第一列是學生名,第二列是學生的成績。
[root@localhost 20190105]# vi students.log jake 85 tom 68 lucy 79 sam 95
如今要對以上文件中的學生成績進行統計,計算學生的數量以及學生的平均成績。經過 while read 語句讀取變量 STUDENT 和 SCORE 的內容,而後在 while 循環中經過 expr 命令計算學生總數和學生總成績,最後計算平均值並輸出。執行該腳本時須要把 students.log 文件的內容重定向到 while2.sh腳本中。
[root@localhost 20190105]# vi while2.sh #!/bin/bash TOTAL=0 //將變量 TOTAL 置 0 COUNT=0 //將變量 COUNT 置 0 #循環讀取數據 while read STUDENT SCORE do #計算總成績 TOTAL=`expr $TOTAL + $SCORE` #計算學生數 COUNT=`expr $COUNT + 1` done #計算平均成績 AVG=`expr $TOTAL / $COUNT` echo 'There are '$COUNT' students , the avg score is '$AVG [root@localhost 20190105]# sh while2.sh < students.log There are 4 students , the avg score is 81 [root@localhost 20190105]#
until是除 for 和 while之外的一種循環結構,它會循環執行一系列命令直到條件爲真時中止。
until循環結構語法:
until 條件 do 命令1 命令2 ... done
until循環中讀取用戶輸入的內容並顯示到屏幕上,當用戶輸入的內容爲 exit 時結束循環。
[root@localhost 20190105]# vi until1.sh #!/bin/bash xxx="" #當 ans 變量的值爲 exit 時結束循環 until [ "$xxx" = exit ] do #讀取用戶的輸入到ans變量 read xxx #若是用戶輸入的不是 exit 則輸出用戶的輸入 if [ "$xxx" != exit ] then echo 'The user input is : '$xxx #不然退出循環 else echo 'Exit the script.' fi done [root@localhost 20190105]# sh until1.sh hello The user input is : hello welcome to HongKong! The user input is : welcome to HongKong! exit Exit the script. [root@localhost 20190105]#