SHELL指令碼的文法知識——流程式控制制

來源:互聯網
上載者:User

標籤:

①if-elif-elif-else-fi

  if-fi

  ` if(($a > 30));then echo $a;fi `       or  ` if [ $a -gt 30 ];then echo $a;fi `

  if-else-fi

  ` if(($a = 30));then echo $a;else echo 30;fi `  or  ` if [ $a -eq 30 ];then echo $a;else echo 30;fi `

  if-elif-elif-else-fi

  ` if(($a > 30)) && (($a = 30));then echo $a;elif(($a > 20)) || (($a = 20));then echo $[$a+1];else echo ‘Sorry‘;fi `

備忘:如果用方括弧[]包裹條件,則if 與 [之間、運算元與[ 、]之間必須有空格
  > *** -gt  = *** -eq  >= *** -ge  <= *** -le  < *** -lt  != *** -ne

  if 判斷文件屬性

  ` if [ -defrwx filename ] `  -d 存在&&目錄  -e 存在  -f 存在&&普通檔案  -r 可讀  -w 可寫  -x 可執行

②case $a in value1) command;; value2) command;; value3) command;; *) command;; esac

  ` read -p ‘Please input a number: ‘ a;

     case $[ $a%2 ] in

    0) 

      echo "The number is even!";;

    1)

      echo "The number is odd!";;

    *)

      echo "This is impossible!";;

     esac `

③for迴圈

  for 變數名 in 迴圈條件;do command[;] done

  eg:  

  a=`seq 1 10`;  

  for i in $a ;do echo $i ;done  or  for i in `seq 1 10`;do echo $i;done  or  for i in 1 2 3 4 5;do echo $i;done

備忘:`seq a b`表示一個從a到b的序列,例如`seq 1 10`產生一個從1到10的序列

④while迴圈

    while 條件;do command;done

  eg:

  a=10;

  while [ $a -ge 1 ];do

    echo $a;

    a=$[$-1];

  done

SHELL指令碼的文法知識——流程式控制制

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.