天天看點

Bash shell 程式設計之基礎知識概述 (if語句)

一、Bash shell是什麼

shell是什麼,Bash與shell又有什麼關系。(以前我也不是特别清楚~~~~)

shell 是一個互動性指令解釋器。shell獨立于作業系統,這種設計讓使用者可以靈活選擇适合自己的shell。shell讓你在指令行鍵入指令,經過shell解釋後傳送給作業系統(核心)執行。

shell是一個指令處理器(command processor)--是一個讀入并解釋你輸入的指令的程式。除了是一個指令中斷器以外,shell還是一個程式設計語言。你可以編寫shell可以解釋的程式(被稱為源程式),這些源程式可以包含shell程式設計指令等等。shell除了解釋指令以外,還有其他工作,它也可以配置和程式設計。  

shell擁有自己的語言允許使用者編寫程式并以一種複雜方式運作。shell程式設計語言具有許多常用的程式設計語言的特征,例如:循環和控制結構等。使用者可以生成像其他應用程式一樣複雜的shell程式。

以下是shell功能的一個彙總:

查找指令的位置并且執行相關聯的程式;

為shell變量賦新值;

執行指令替代;

處理 I/O重定向和管道功能;

提供一個解釋性的程式設計語言界面,包括tests、branches和loops等語句。

bash是borne again shell的縮寫,它是shell的一種,Linux上預設采用的是bash。當然還有sh,dash,tcsh和ksh等

1、讀入變量(read)

read指令是用于從終端或者檔案中讀取輸入的内建指令,read指令讀取整行輸入,每行末尾的換行符不被讀入。在read指令後面,如果沒有指定變量名,讀取的資料将被自動指派給特定的變量REPLY。下面的清單給出了read指令的常用方式:

read

從标準輸入中讀取一行并指派給特定的變量ERPLY(沒有指定變量名的話)

read one

從标準輸入讀取并指派給變量one

read first last

從标準輸入讀取輸入到第一個空格或者回車,将輸入的第一個單詞放到變量first中,并将該行其他的輸入放在變量last中。

read -p

列印提示資訊,等待輸入,并指派給預設的變量REPLY

read -t

設定逾時時間(機關:秒)

read -a arry_name

将輸入在清單存入到"arry_name"數組中

read示例:

1

2

3

4

5

<code>#無變量,預設存于特定變量REPLY中</code>

<code>[root@localhost apache]</code><code># read    #等待控制台輸入,并将結果指派給特定内置變量REPLY。</code>

<code>hello           </code><code>#控制台輸入Hello</code>

<code>[root@localhost apache]</code><code># echo $REPLY  #列印變量</code>

<code>hello</code>

<code>[root@localhost apache]</code><code># read one two three</code>

<code>3 9 8  </code><code>#輸入1 2 3,它們之間用空格隔開。</code>

<code>[root@localhost apache]</code><code># echo "one is:$one,two is:$two three is:$three"</code>

<code>#列印結果</code>

<code>one is:3,two is:9 three is:8</code>

<code>[root@localhost apache]</code><code># read -p "Please input your name:" name</code>

<code>#輸出"Please input your name"文本提示,同時等待輸入,并将結果指派給name</code>

<code>Please input your name:scott</code>

<code>[root@localhost apache]</code><code># echo "$name"</code>

<code>scott</code>

<code>[root@localhost apache]</code><code># read -t 4  -p "Enter your nmber:"</code>

<code>#限時4秒輸入,如果過了4秒,将退出不再輸入</code>

<code>Enter your nmber:[root@localhost apache]</code><code># echo $REPLY</code>

<code>#結果為空</code>

2.  狀态判斷:

test是Shell中提供的内置指令,主要用于狀态的檢驗,如果結果為0,表示成功,否則表示失敗。

<code>[root@localhost apache]</code><code># name=scott</code>

<code>[root@localhost apache]</code><code># test $name != scoot</code>

<code>[root@localhost apache]</code><code># echo $? #測試上一條指令執行狀态的傳回值,0表示功</code>

<code>0</code>

注意的是test指令不支援Shell中提供的各種通配符

<code>[root@localhost apache]</code><code># name=tom</code>

<code>[root@localhost apache]</code><code># test $name = [Tt]om</code>

<code>[root@localhost apache]</code><code># echo $?</code>

<code>1</code>

test指令還可以中括号予以替換,其語義保持不變

<code>[root@localhost apache]</code><code># [ "$name" = tom  ]</code>

在Shell中還提供了另外一種用于狀态判斷的方式:` expr `,和test不同的是,該方式中的表達式支援通配符

<code>[root@localhost apache]# [[ $name==[tT]om ]]</code>

<code>[root@localhost apache]# echo $?</code>

在` expression `中,expression可以包含&amp;&amp;(邏輯與)和||(邏輯或)。

<code>[root@localhost apache]</code><code># friend=Jack</code>

<code>[root@localhost apache]</code><code># [[ $name==[tT]om &amp;&amp; $friend == "Jack" ]]</code>

在Shell中還提供了let指令的判斷方式: (( expr ))

6

7

8

<code>[root@localhost apache]</code><code># a=45</code>

<code>[root@localhost apache]</code><code># b=34</code>

<code>[root@localhost apache]</code><code># (( a &gt; b ))</code>

<code>[root@localhost apache]</code><code># (( a == 45 &amp;&amp; b == 34 ))</code>

下面的表格是test指令支援的操作符:

表達式

判斷為真的條件

字元串判斷

結果

[ StringA=String ]

StringA等于StringB

[ StringA==StringB ]

[ StringA!=StringB ]

StringA不等于StringB

[ String ]

String不為空

[ -z String ]

String長度為0

[ -n String ]

String長度不為0

邏輯判斷

[ StringA -a StringB ]

StringA和StringB都是真

[ StringA -o StringB ]

StringA或StringB是真

[ !String ]

String不為真

邏輯判斷(複合判斷)

[[ pattern1 &amp;&amp; pattern2 ]]

pattern1和pattern2都是真

[[ pattern1 || pattern2 ]]

pattern1或pattern2是真

[[ !pattern ]]

pattern不為真

整數判斷

[ intA -eq intB ]

intA等于intB

[ intA -ne intB ]

intA不等于intB

[ intA -ge intB ]

intA大于等于intB

[ intA -lt intB ]

intA小于intB

[ intA -le intB ]

intA小于等于intB

[ intA -gt intB ]

intA大于intB

檔案判斷中的二進制操作

[ fileA -ot fileB ]

fileA比fileB舊

[ fileA -ef fileB ]

fileA和fileB有相同的裝置或者inode值

[ fileA -nt fileB ]

fileA比fileB新

檔案檢驗

[ -d $file ] or [[ -d $file ]]

file為目錄且存在時為真

[ -e $file ] or [[ -e $file ]]

file為檔案且存在時為真

[ -f $file ] or [[ -f $file ]]

file為非目錄普通檔案存在時為真

[ -s $file ] or [[ -s $file ]]

file檔案存在, 且長度不為0時為真

[ -L $file ] or [[ -L $file ]]

file為連結符且存在時為真

[ -r $file ] or [[ -r $file ]]

file檔案存在且可讀時為真

[ -w $file ] or [[ -w $file ]]

file檔案存在且可寫時為真

[ -x $file ] or [[ -x $file ]]

file檔案存在且可執行時為真

[ -S $file ] or [[ -S $file ]]

測試檔案是否存在在并且是否是一個套接字檔案

[ -h $file ] or [[ -h $file ]]

[ -p $file ] or [[ -p $file ]]

測試檔案是否存在并且是否是一個管道檔案

注:在邏輯判斷(複合判斷中),pattern可以包含元字元,在字元串的判斷中,pattern2必須被包含在引号中。

3.流程控制語句:

if語句格式如下:

if語句的後面是Shell指令,如果該指令執行成功傳回0,則執行then後面的指令。

if command;then

       command

 fi

用test指令測試其後面expression的結果,如果為真,則執行then後面的指令。

   if test expression

   then

   fi

下面的格式和test expression等同

   if [ string/numeric expression ]

下面的兩種格式也可以用于判斷語句的條件表達式,而且它們也是目前比較常用的兩種。

   if ` string expression `

       .........

   if (( numeric expression ))           

        .......

示例:

9

10

11

<code>[root@localhost tmp]</code><code># cat test2.sh</code>

<code>#!/bin/bash</code>

<code>read</code> <code>-p  </code><code>"Are you OK(y/n)?"</code> <code>answer</code>

<code>#這裡的$answer變量必須要用雙引号擴住,否則判斷将失敗</code>

<code>   </code><code>if</code> <code>[ </code><code>"$answer"</code> <code>= y -o </code><code>"$answer"</code> <code>= Y ]</code>

<code>    </code><code>then</code>

<code>        </code><code>echo</code> <code>"Glad to see it."</code>

<code>    </code><code>fi</code>

<code>[root@localhost tmp]</code><code># bash test2.sh</code>

<code>Are you OK(y</code><code>/n</code><code>)?y</code>

<code>Glad to see it.</code>

上面的判斷還可以替換為:

<code>read</code>  <code>-p </code><code>"Are you OK(y/n or Maybe)?"</code> <code>answer</code>

<code>#` `複合指令操作符允許其中的表達式包含元字元,這裡輸入以y或Y開頭的任意單詞,或Maybe都執行then後面的echo。</code>

<code>if</code> <code>[[ $answer == [yY]* || $answer = Maybe ]];</code><code>then</code>

<code>        </code><code>echo</code> <code>"Glad to hear it."</code>

<code>Are you OK(y</code><code>/n</code> <code>or Maybe)?yadfadsf</code>

<code>Glad to hear it.</code>

<code>[root@localhost tmp]</code><code># cat test3.sh</code>

<code> </code><code>answer=</code><code>"not really"</code>

<code>    </code><code>if</code> <code>[[ $answer = [Nn]o?( way |t really) ]]</code>

<code>     </code><code>then</code>

<code>        </code><code>echo</code> <code>"I am sorry."</code>

<code>     </code><code>fi</code>

<code>[root@localhost tmp]</code><code># bash -n test3.sh</code>

<code>[root@localhost tmp]</code><code># bash test3.sh</code>

<code>I am sorry.</code>

<code>#對于本示例中的擴充通配符,這裡需要給出一個具體的解釋。[Nn]o比對No或no,?( way|t really)則表示0個或1個( way或t really),是以answer變量比對的字元串為No、no、Not really、not really、No way、no way。</code>

if/elif/else語句的使用方式和if語句極為相似,其格式如下:

   if command

   elif command

   else

12

13

14

15

16

17

18

19

20

21

22

23

<code>[root@localhost tmp]</code><code># cat test4.sh</code>

<code> </code><code>read</code>  <code>-p </code><code>"How old are you?"</code> <code>age</code>

<code>    </code><code>if</code> <code>[ $age -lt 0 -o $age -gt 120 ]                </code><code>#(( age &lt; 0 || age &gt; 120 ))</code>

<code>        </code><code>echo</code> <code>"You are so old."</code>

<code>    </code><code>elif</code> <code>[ $age -</code><code>ge</code> <code>0 -a $age -</code><code>le</code> <code>12 ]               </code><code>#(( age &gt;= 0 &amp;&amp; age &lt;= 12 ))</code>

<code>        </code><code>echo</code> <code>"You are child."</code>

<code>    </code><code>elif</code> <code>[ $age -</code><code>ge</code> <code>13 -a $age -</code><code>le</code> <code>19 ]            </code><code># (( age &gt;= 13 &amp;&amp; age &lt;= 19 ))</code>

<code>        </code><code>echo</code> <code>"You are 13--19 years old."</code>

<code>    </code><code>elif</code> <code>[ $age -</code><code>ge</code> <code>20 -a $age -</code><code>le</code> <code>29 ]           </code><code># ((age &gt;= 20 &amp;&amp; age &lt;= 29 ))</code>

<code>        </code><code>echo</code> <code>"You are 20--29 years old."</code>

<code>    </code><code>elif</code> <code>[ $age -</code><code>ge</code> <code>30 -a $age -</code><code>le</code> <code>39 ]             </code><code># (( age &gt;= 30   &amp;&amp; age &lt;= 39 ))</code>

<code>        </code><code>echo</code> <code>"You are 30--39 years old."</code>

<code>    </code><code>else</code>

<code>        </code><code>echo</code> <code>"You are above 40."</code>

<code>[root@localhost tmp]</code><code># bash test4.sh</code>

<code>How old are you?60</code>

<code>You are above 40.</code>

=======================================完================================================

本文轉自 jinlinger 51CTO部落格,原文連結:http://blog.51cto.com/essun/1362838,如需轉載請自行聯系原作者