條件判斷:if語句
語法格式:
if [ expression ]
then
Statement(s) to be executed if expression is true
fi
注意:expression 和方括號([ ])之間必須有空格,否則會有語法錯誤。
if 語句通過關系運算符判斷表達式的真假來決定執行哪個分支。Shell 有三種 if ... else 語句:
if ... fi 語句
if ... else ... fi 語句
if ... elif ... else ... fi 語句
示例:
#!/bin/bash/
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater to b"
else
echo "a is less to b"
fi
if ... else 語句也可以寫成一行,以命令的方式來運行:
a=10;b=20;if [ $a == $b ];then echo "a is equal to b";else echo "a is not equal to b";fi;
if ... else 語句也經常與 test 命令結合使用,作用與上面一樣:
#!/bin/bash/
a=10
b=20
if test $a == $b
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
分支控制:case語句
case ... esac 與其他語言中的 switch ... case 語句類似,是一種多分枝選擇結構。
示例:
#!/bin/bash/
grade="B"
case $grade in
"A") echo "Very Good!";;
"B") echo "Good!";;
"C") echo "Come On!";;
*)
echo "You Must Try!"
echo "Sorry!";;
esac
轉換成C語言是:
#include stdio.h>
int main(){
char grade = 'B';
switch(grade){
case 'A': printf("Very Good!");break;
case 'B': printf("Very Good!");break;
case 'C': printf("Very Good!");break;
default:
printf("You Must Try!");
printf("Sorry!");
break;
}
return 0;
}
對比看就很容易理解了。很相似,只是格式不一樣。
需要注意的是:
取值后面必須為關鍵字 in,每一模式必須以右括號結束。取值可以為變量或常數。匹配發現取值符合某一模式后,其間所有命令開始執行直至 ;;。;; 與其他語言中的 break 類似,意思是跳到整個 case 語句的最后。
取值將檢測匹配的每一個模式。一旦模式匹配,則執行完匹配模式相應命令后不再繼續其他模式。如果無一匹配模式,使用星號 * 捕獲該值,再執行后面的命令。
再舉一個例子:
#!/bin/bash
option="${1}"
case ${option} in
"-f") FILE="${2}"
echo "File name is $FILE"
;;
"-d") DIR="${2}"
echo "Dir name is $DIR"
;;
*)
echo "`basename ${0}`:usage: [-f file] | [-d directory]"
exit 1 # Command to come out of the program with status 1
;;
esac
運行結果:
$./test.sh
test.sh: usage: [ -f filename ] | [ -d directory ]
./test.sh -f index.html
File name is index.html
這里用到了特殊變量${1},指的是獲取命令行的第一個參數。
for循環
shell的for循環與c、php等語言不同,同Python很類似。下面是語法格式:
for 變量 in 列表
do
command1
command2
...
commandN
done
示例:
#!/bin/bash/
for value in 1 2 3 4 5
do
echo "The value is $value"
done
輸出:
The value is 1
The value is 2
The value is 3
The value is 4
The value is 5
順序輸出字符串中的字符:
for str in 'This is a string'
do
echo $str
done
運行結果:
This is a string
遍歷目錄下的文件:
#!/bin/bash
for FILE in *
do
echo $FILE
done
上面的代碼將遍歷當前目錄下所有的文件。在Linux下,可以改為其他目錄試試。
遍歷文件內容:
city.txt
beijing
tianjin
shanghai
#!/bin/bash
citys=`cat city.txt`
for city in $citys
echo $city
done
輸出:
beijing
tianjin
shanghai
while循環
只要while后面的條件滿足,就一直執行do里面的代碼塊。
其格式為:
while command
do
Statement(s) to be executed if command is true
done
命令執行完畢,控制返回循環頂部,從頭開始直至測試條件為假。
示例:
#!/bin/bash
c=0;
while [ $c -lt 3 ]
do
echo "Value c is $c"
c=`expr $c + 1`
done
輸出:
Value c is 0
Value c is 1
Value c is 2
這里由于shell本身不支持算數運算,所以使用expr命令進行自增。
until循環
until 循環執行一系列命令直至條件為 true 時停止。until 循環與 while 循環在處理方式上剛好相反。一般while循環優于until循環,但在某些時候,也只是極少數情況下,until 循環更加有用。
將上面while循環的例子改改,就能達到一樣的效果:
#!/bin/bash
c=0;
until [ $c -eq 3 ]
do
echo "Value c is $c"
c=`expr $c + 1`
done
首先do里面的語句塊一直在運行,直到滿足了until的條件就停止。
輸出:
Value c is 0
Value c is 1
Value c is 2
跳出循環
在循環過程中,有時候需要在未達到循環結束條件時強制跳出循環,像大多數編程語言一樣,Shell也使用 break 和 continue 來跳出循環。
break
break命令允許跳出所有循環(終止執行后面的所有循環)。
#!/bin/bash
i=0
while [ $i -lt 5 ]
do
i=`expr $i + 1`
if [ $i == 3 ]
then
break
fi
echo -e $i
done
運行結果:
1
2
在嵌套循環中,break 命令后面還可以跟一個整數,表示跳出第幾層循環。例如:
break n
表示跳出第 n 層循環。
continue
continue命令與break命令類似,只有一點差別,它不會跳出所有循環,僅僅跳出當前循環。
#!/bin/bash
i=0
while [ $i -lt 5 ]
do
i=`expr $i + 1`
if [ $i == 3 ]
then
continue
fi
echo -e $i
done
運行結果:
1
2
4
5
以上內容是小編給大家介紹的Shell腳本的條件控制和循環語句的相關知識,希望對大家有所幫助!
您可能感興趣的文章:- shell腳本中case條件控制語句的一個bug分析
- shell腳本編程之循環語句
- shell腳本編程之循環語句學習筆記
- Shell腳本while、until循環語句簡明教程
- Shell腳本for循環語句簡明教程
- shell中的循環語句、判斷語句實例
- Shell中的循環語句for、while、until實例講解
- shell基礎學習中的字符串操作、for循環語句示例
- 探索PowerShell(十) 循環語句介紹