linux學習之shell指令碼 ------- 控制流程結構

來源:互聯網
上載者:User

標籤:shell控制流程結構   until   while   for   break   

[本文是自己學習所做筆記,歡迎轉載,但請註明出處:http://blog.csdn.net/jesson20121020]

今天開始學一些同其他進階語言一樣的shell流量控制結構
流量控制語句:  1. if語句

   語句格式:    

if condition1      then   command1else condition2then   command2else   command3fi
  註:if語句必須以fi終止。

  如果沒有condition2,則if語句可以簡化為如下:

if conditionthen       command1else       command2fi
或:

if conditionthen       command1fi

   例子:

if_test.sh

#!/bin/bash#if_testecho -n "please input two number(a and b):"read a bif [ $a -lt $b ]then   echo "$a is less than $b"elif [ $a -gt $b ]then   echo "$a is greater than $b"else   echo "$a ia equal to $b"fi

  給予可執行許可權,執行該指令碼:

[email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx if_test.sh[email protected]:~/develop/worksapce/shell_workspace$ ./if_test.sh please input two number(a and b):10 1010 ia equal to 10[email protected]:~/develop/worksapce/shell_workspace$ ./if_test.sh please input two number(a and b):10 1210 is less than 12[email protected]:~/develop/worksapce/shell_workspace$ ./if_test.sh please input two number(a and b):8 28 is greater than 2
  注意,其中用於了比較子,更加詳細的比較子,請參考man test。


  2. case語句

   語句格式:

    case value in

    模式1)

      command1

      ;;

    模式2)

      command2

      ;;

    esac


   注意:

    1case取值後面必須為單詞in,每一模式必須以右括弧結束,取值可以為變數或者常數。

     2 模式比對符*表示任一字元;?表示任意單字元;[...]表示類或範圍中任一字元。

   例子:

case_test.sh

#!/bin/bash#case_testecho -n "Enter a number from 1 to 3:"read ncase $n in1)    echo "You select 1"    ;;2)    echo "You select 2"    ;;3)    echo "You select 3"    ;;*)  echo "You select *"    ;;esac

[email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx case_test.sh [email protected]:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:1You select 1[email protected]:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:2You select 2[email protected]:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:3You select 3[email protected]:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:*You select *[email protected]:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:20You select *
  3. for迴圈

   格式:

    for 變數名 in 列表

    do

     命令1

     命令2

    done

   例子:

for_test1.sh

#!/bin/bash#for_testfor loop in 1 2 3 4 5do    echo $loopdone
  運行結果:

[email protected]:~/develop/worksapce/shell_workspace$ ./for_test1.sh 12345
for_test2.sh

#!/bin/bash#for_test2for loop in orange red blue graydo   echo $loopdone
  運行結果:

[email protected]:~/develop/worksapce/shell_workspace$ ./for_test2.sh orangeredbluegray
for_test3.sh

#!/bin/bash#for_test3for loop in `cat name.txt`do    echo $loopdone
  運行結果:

[email protected]:~/develop/worksapce/shell_workspace$ cat name.txt jessoncherry[email protected]:~/develop/worksapce/shell_workspace$ ./for_test3.sh jessoncherry

  4. until迴圈

   格式:

    until 條件

    do

      命令

    done

until_test.sh

#!/bin/bash#until_testi=10until [ $i -lt 0 ]do    echo $i    let i=i-1   # ((i=$i-1))   #i=$[i-1]done
  給予許可權,執行結果如下:

[email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx until_test.sh [email protected]:~/develop/worksapce/shell_workspace$ ./until_test.sh 109876543210
  

  5. while迴圈

   格式:

    while 命令

    do

       命令

      .......

    done

   例子:

while_test1.sh

#!/bin/bash#while_test1while echo -n "輸入你喜歡的明星:";read Mingxingdo    echo "$Mingxing 是你喜歡的明星"done
  給予許可權,執行:

[email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx while_test1.sh [email protected]:~/develop/worksapce/shell_workspace$ ./while_test1.sh 輸入你喜歡的明星:周傑周傑 是你喜歡的明星輸入你喜歡的明星:孫楠孫楠 是你喜歡的明星
while_test2.sh

#!/bin/bash#while_testwhile read NAMEdo   echo $NAMEdone <name.txt
  給予許可權,執行:

[email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx while_test2.sh [email protected]:~/develop/worksapce/shell_workspace$ ./while_test2.sh jessoncherry

  6. break和continue

   break [n]

   -退出迴圈

   -如果是在一個嵌入迴圈裡,可以指定n來跳出的迴圈個數。

   continue

   -跳過迴圈步

   注意,這和其他進階語言中的一樣,continue命令類似於break,只有一點差別,它不會跳出迴圈,而是路過當前迴圈步。

   例子:

break_test.sh

#!/bin/bash#break_test.shwhile :do    echo -n "Enter any number(1,...,5):"    read ANS    case $ANS in    1|2|3|4|5)        echo "You enter anumber between 1 and 5"        ;;    *)        echo "Wrong number,Bye"        break;        ;;    esacdone
   給予許可權,執行如下:

[email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx break_test.sh [email protected]:~/develop/worksapce/shell_workspace$ ./break_test.sh Enter any number(1,...,5):1You enter anumber between 1 and 5Enter any number(1,...,5):2You enter anumber between 1 and 5Enter any number(1,...,5):3You enter anumber between 1 and 5Enter any number(1,...,5):4You enter anumber between 1 and 5Enter any number(1,...,5):5You enter anumber between 1 and 5Enter any number(1,...,5):6Wrong number,Bye

continue_test.sh

#!/bin/bash#continue_break_testwhile :do     echo -n "Enter any number(1,...,5):"     read ANS     case $ANS in     1|2|3|4|5)         echo "You enter a number between 1 and 5"         ;;     *)         echo -n "Wrong number,continue(y/n)?:"         read IS_CONTINUE         case $IS_CONTINUE in         y|yes|Y|Yes)                continue                ;;         *)                break                ;;         esac     esacdone
  同樣,給予可執行許可權,執行

[email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx continue_test.sh [email protected]:~/develop/worksapce/shell_workspace$ ./continue_test.sh Enter any number(1,...,5):2You enter a number between 1 and 5Enter any number(1,...,5):3You enter a number between 1 and 5Enter any number(1,...,5):4You enter a number between 1 and 5Enter any number(1,...,5):5You enter a number between 1 and 5Enter any number(1,...,5):8Wrong number,continue(y/n)?:yEnter any number(1,...,5):1You enter a number between 1 and 5Enter any number(1,...,5):79Wrong number,continue(y/n)?:n

 



linux學習之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.