Linux shell 流程控制(條件if,循環for,while)

linux shell有一套自己的流程控制語句,其中包括條件語句(if),循環語句(for,while),選擇語句(case)。下面我將通過例子介紹下,各個語句使用方法。

一、shell條件語句(if用法)

if語句結構[if/then/elif/else/fi]

if 條件測試語句

then

action

[elif 條件

action

else

action

]

fi


如果對於:條件測試語句不是很清楚,可以參考:linux shell 邏輯運算符、邏輯表達式詳解,也可以參考下面的參數。

一、邏輯運算符


邏輯卷標表示意思
1.關於檔案與目錄的偵測邏輯卷標!
-f常用!偵測『檔案』是否存在 eg: if [ -f filename ]
-d常用!偵測『目錄』是否存在
-b偵測是否爲一個『 block 檔案』
-c偵測是否爲一個『 character 檔案』
-S偵測是否爲一個『 socket 標籤檔案』
-L偵測是否爲一個『 symbolic link 的檔案』
-e偵測『某個東西』是否存在!
2.關於程序的邏輯卷標!
-G偵測是否由 GID 所執行的程序所擁有
-O偵測是否由 UID 所執行的程序所擁有
-p偵測是否爲程序間傳送信息的 name pipe 或是 FIFO (老實說,這個不太懂!)
3.關於檔案的屬性偵測!
-r偵測是否爲可讀的屬性
-w偵測是否爲可以寫入的屬性
-x偵測是否爲可執行的屬性
-s偵測是否爲『非空白檔案』
-u偵測是否具有『 SUID 』的屬性
-g偵測是否具有『 SGID 』的屬性
-k偵測是否具有『 sticky bit 』的屬性
4.兩個檔案之間的判斷與比較 ;例如[ test file1 -nt file2 ]
-nt第一個檔案比第二個檔案新
-ot第一個檔案比第二個檔案舊
-ef第一個檔案與第二個檔案爲同一個檔案( link 之類的檔案)
5.邏輯的『和(and)』『或(or)』
&&邏輯的 AND 的意思
||邏輯的 OR 的意思



運算符號代表意義
=等於 應用於:整型或字符串比較 如果在[] 中,只能是字符串
!=不等於 應用於:整型或字符串比較 如果在[] 中,只能是字符串
<小於 應用於:整型比較 在[] 中,不能使用 表示字符串
>大於 應用於:整型比較 在[] 中,不能使用 表示字符串
-eq等於 應用於:整型比較
-ne不等於 應用於:整型比較
-lt小於 應用於:整型比較
-gt大於 應用於:整型比較
-le小於或等於 應用於:整型比較
-ge大於或等於 應用於:整型比較
-a雙方都成立(and) 邏輯表達式 –a 邏輯表達式
-o單方成立(or) 邏輯表達式 –o 邏輯表達式
-z空字符串
-n非空字符串


shell命令,可以按照分號分割,也可以按照換行符分割。如果想一行寫入多個命令,可以通過“';”分割。

如:

[chengmo@centos5 ~]$ a=5;if ` a -gt 4 ` ;then echo 'ok';fi;                        
ok

實例:(test.sh)

#!/bin/sh

scores=40;
if [[ $scores -gt 90 ]]; then
   echo "very good!";
elif [[ $scores -gt 80 ]]; then
   echo "good!";
elif [[ $scores -gt 60 ]]; then
   echo "pass!";
else
   echo "no pass!";
fi;

p_w_picpath

條件測試有:[[]],[],test 這幾種,注意:[[]] 與變量之間用空格分開。

二、循環語句(for,while,until用法):

  • for循環使用方法(for/do/done)

語法結構:

1.for … in 語句

for 變量 in seq字符串

do

action

done

說明:seq字符串 只要用空格字符分割,每次for…in 讀取時候,就會按順序將讀到值,給前面的變量。

實例(testfor.sh):

#!/bin/sh

for i in $(seq 10); do
   echo $i;
done;

p_w_picpath

seq 10 產生 1 2 3 。。。。10空格分隔字符串。

2.for((賦值;條件;運算語句))

for((賦值;條件;運算語句))

do

action

done;

實例(testfor2.sh):

#!/bin/sh

for((i=1;i<=10;i++));do
   echo $i;
done;

p_w_picpath

  • while循環使用(while/do/done)

while語句結構

while 條件語句

do

action

done;

實例1:

#!/bin/sh
i=10;
while [[ $i -gt 5 ]];do
   echo $i;
   ((i--));
done;

運行結果:========================

sh testwhile1.sh
10
9
8
7
6

實例2:(循環讀取文件內容:)

#!/bin/sh

while read line;do
   echo $line;
done < /etc/hosts;

運行結果:===================

sh testwhile2.sh


# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 centos5 localhost.localdomain localhost

  • until循環語句

語法結構:

until 條件

do

action

done

意思是:直到滿足條件,就退出。否則執行action.

實例(testuntil.sh):

#!/bin/sh

a=10;

until [[ $a -lt 0 ]];do

echo $a;

((a—));

done;

結果:

sh testuntil.sh

10
9
8
7
6
5
4
3
2
1
0

三、shell選擇語句(case、select用法)

  • case選擇語句使用(case/esac)

語法結構

case $arg in  
   pattern | sample) # arg in pattern or sample  
   ;;  
   pattern1) # arg in pattern1  
   ;;  
   *) #default  
   ;;  
esac

說明:pattern1 是正則表達式,可以用下面字符:

                *       任意字串
                ?       任意字元
                [abc]   a, b, 或c三字元其中之一
                [a-n]   從a到n的任一字元
                |       多重選擇

實例:

#!/bin/sh

case $1 in
start | begin)
   echo "start something"  
   ;;
stop | end)
   echo "stop something"  
   ;;
*)
   echo "Ignorant"  
   ;;
esac

運行結果:======================

testcase.sh start
start something

  • select語句使用方法(產生菜單選擇)

語法:

select 變量name  in seq變量

do

   action

done

實例:

#!/bin/sh

select ch in "begin" "end" "exit"
do
case $ch in
"begin")
   echo "start something"  
   ;;
"end")
   echo "stop something"  
   ;;
"exit")
   echo "exit"  
   break;
   ;;
*)
   echo "Ignorant"  
   ;;
esac
done;

運行結果:

p_w_picpath

說明:select是循環選擇,一般與case語句使用。

這篇文章非常好 值得轉摘,原文地址爲:

http://www.cnblogs.com/chengmo/archive/2010/10/14/1851434.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章