在Linux系統(tǒng)中Shell腳本使用if語(yǔ)句的方法-創(chuàng)新互聯(lián)

這篇文章主要講解了“在Linux系統(tǒng)中Shell腳本使用if語(yǔ)句的方法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“在Linux系統(tǒng)中Shell腳本使用if語(yǔ)句的方法”吧!

創(chuàng)新互聯(lián)是一家以網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、品牌設(shè)計(jì)、軟件運(yùn)維、成都網(wǎng)站推廣、小程序App開(kāi)發(fā)等移動(dòng)開(kāi)發(fā)為一體互聯(lián)網(wǎng)公司。已累計(jì)為邊坡防護(hù)網(wǎng)等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開(kāi)發(fā)服務(wù)。

Bourne Shell 的 if 語(yǔ)句和大部分編程語(yǔ)言一樣 - 檢測(cè)條件是否真實(shí),如果條件為真,shell 會(huì)執(zhí)行這個(gè) if 語(yǔ)句指定的代碼塊,如果條件為假,shell 就會(huì)跳過(guò) if 代碼塊,繼續(xù)執(zhí)行之后的代碼。
在Linux系統(tǒng)中Shell腳本使用if語(yǔ)句的方法

if 語(yǔ)句的語(yǔ)法:


代碼如下:

if [ 判斷條件 ]
   then
           command1
           command2
           ……..
           last_command
   fi</p> <p>Example:</p> <p>    #!/bin/bash
   number=150
   if [ $number -eq 150 ]
   then
       echo "Number is 150"
   fi

if-else 語(yǔ)句:

除了標(biāo)準(zhǔn)的 if 語(yǔ)句之外,我們還可以加入 else 代碼塊來(lái)擴(kuò)展 if 語(yǔ)句。這么做的主要目的是:如果 if 條件為真,執(zhí)行 if 語(yǔ)句里的代碼塊,如果 if 條件為假,執(zhí)行 else 語(yǔ)句里的代碼塊。
語(yǔ)法:


代碼如下:

if [ 判斷條件 ]
   then
          command1
          command2
          &hellip;&hellip;..
          last_command
   else
          command1
          command2
          &hellip;&hellip;..
          last_command
   fi

Example:


代碼如下:

#!/bin/bash
   number=150
   if [ $number -gt 250 ]
   then
       echo "Number is greater"
   else
       echo "Number is smaller"
   fi

If..elif..else..fi 語(yǔ)句 (簡(jiǎn)寫(xiě)的 else if)

Bourne Shell 的 if 語(yǔ)句語(yǔ)法中,else 語(yǔ)句里的代碼塊會(huì)在 if 條件為假時(shí)執(zhí)行。我們還可以將 if 語(yǔ)句嵌套到一起,來(lái)實(shí)現(xiàn)多重條件的檢測(cè)。我們可以使用 elif 語(yǔ)句(else if 的縮寫(xiě))來(lái)構(gòu)建多重條件的檢測(cè)。
語(yǔ)法 :


代碼如下:

if [ 判斷條件1 ]
   then
          command1
          command2
          &hellip;&hellip;..
          last_command
   elif [ 判斷條件2 ]
   then
           command1
           command2
           &hellip;&hellip;..
           last_command
   else
   command1
   command2
   &hellip;&hellip;..
   last_command
   fi

Example :


代碼如下:

#!/bin/bash
   number=150
   if [ $number -gt 300 ]
   then
       echo "Number is greater"
   elif [ $number -lt 300 ]
   then
       echo "Number is Smaller"
   else
       echo "Number is equal to actual value"
   fi

多重 if 語(yǔ)句 :

If 和 else 語(yǔ)句可以在一個(gè) bash 腳本里相互嵌套。關(guān)鍵詞 “fi” 表示里層 if 語(yǔ)句的結(jié)束,所有 if 語(yǔ)句必須使用 關(guān)鍵詞 “fi” 來(lái)結(jié)束。

基本 if 語(yǔ)句的嵌套語(yǔ)法:


代碼如下:

if [ 判斷條件1 ]
   then
           command1
           command2
           &hellip;&hellip;..
           last_command
   else
   if [ 判斷條件2 ]
   then
           command1
           command2
           &hellip;&hellip;..
           last_command
   else
           command1
           command2
            &hellip;&hellip;..
            last_command
         fi
   fi

Example:


代碼如下:

#!/bin/bash
   number=150
   if [ $number -eq 150 ]
   then
      echo "Number is 150"
   else
   if [ $number -gt 150 ]
   then
       echo "Number is greater"
   else
       echo "'Number is smaller"
      fi
   fi

感謝各位的閱讀,以上就是“在Linux系統(tǒng)中Shell腳本使用if語(yǔ)句的方法”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)在Linux系統(tǒng)中Shell腳本使用if語(yǔ)句的方法這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

分享名稱(chēng):在Linux系統(tǒng)中Shell腳本使用if語(yǔ)句的方法-創(chuàng)新互聯(lián)
URL鏈接:http://muchs.cn/article2/egcic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站維護(hù)、微信小程序、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷(xiāo)型網(wǎng)站建設(shè)