Shell流程式控制制語句

來源:互聯網
上載者:User


Shell流程式控制制語句  和其他進階語言一樣,Shell提供了用來控製程序流程的命令,包括條件分支和迴圈結構,使用者可以用這些命令建立非常複雜的程式  www.2cto.com  與傳統語言不同的是,Shell用於指定條件值的不是布爾運算式,而是,命令和字串。       linux shell有一套自己的流程式控制制語句,其中包括條件陳述式(if),迴圈語句(for,while),選擇語句(case) 一、shell條件陳述式(if用法)if語句結構[if/then/elif/else/fi] if 條件測試語句 thenaction[elif 條件actionelseaction]fi 如果對於:條件測試語句不是很清楚,可以參考:linux shell 邏輯運算子、邏輯運算式詳解shell命令,可以按照分號分割,也可以按照分行符號分割。如果想一行寫入多個命令,可以通過“';”分割。如:[root@test8 test]# 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;  條件測試有:[[]],[],test 這幾種,注意:[[]] 與變數之間用空格分開。  二、迴圈語句(for,while,until用法):• for迴圈使用方法(for/do/done) 文法結構: 1.for … in 語句for 變數 in seq字串doactiondone說明:seq字串 只要用空白字元分割,每次for…in 讀取時候,就會按順序將讀到值,給前面的變數。執行個體(testfor.sh):#!/bin/sh for i in $(seq 10); do    echo $i;done; 2.for((賦值;條件;運算語句)) for((賦值;條件;運算語句)) do action done; 執行個體(testfor2.sh): #!/bin/sh for((i=1;i<=10;i++));do    echo $i;done; • while迴圈使用(while/do/done) while語句結構while 條件陳述式doactiondone;執行個體1:#!/bin/shi=10;while [[ $i -gt 5 ]];do    echo $i;    ((i--));done; 執行個體2:(迴圈讀取檔案內容:)#!/bin/sh while read line;do    echo $line;done < /etc/hosts;  until迴圈語句   www.2cto.com  文法結構:until 條件do actiondone意思是:直到滿足條件,就退出。否則執行action.執行個體(testuntil.sh):#!/bin/sha=10;until [[ $a -lt 0 ]];doecho $a;((a—));done; 結果: 三、shell選擇語句(case、select用法)• case選擇語句使用(case/esac) 文法結構case $arg in      pattern | sample) # arg in pattern or sample      ;;      pattern1) # arg in pattern1      ;;      *) #default      ;;  esac  說明:pattern1 是Regex,可以用下面字元:                 *       任意字串                 ?       任意字元                 [abc]   a, b, 或c三字元其中之一                 [a-n]   從a到n的任一字元                 |       多重選取   執行個體:#!/bin/sh  case $1 instart | begin)    echo "start something"      ;;stop | end)    echo "stop something"      ;;*)    echo "Ignorant"      ;;esac 運行結果:======================     www.2cto.com  testcase.sh startstart something• select語句使用方法(產生菜單選擇) 文法:select 變數name  in seq變數 do     action done執行個體:#!/bin/sh  select ch in "begin" "end" "exit"docase $ch in"begin")    echo "start something"      ;;"end")    echo "stop something"      ;;"exit")    echo "exit"      break;    ;;*)    echo "Ignorant"      ;;esacdone; 運行結果:說明:select是迴圈選擇,一般與case語句使用。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.