linux shell基礎編程2,linuxshell基礎編程

來源:互聯網
上載者:User

linux shell基礎編程2,linuxshell基礎編程

while迴圈

  文法1:

    while [ 條件 ]

              do

    命令序列

    done

  文法2:

    while read -r line

    do 

    命令序列

    done

(切記while和左中括弧一定要有空格)

例子

1 #!/bin/bash2 j=13 SUM=04 while [ $j -lt 10 ]5 do6 SUM=$((SUM+j))7 j=$[j+1]8 done9 echo $SUM

 

if判斷語句

  文法1:

    if 條件

    then

      命令序列

    fi

  文法2:

    if 條件

    then

      條件序列

    else

      條件序列

    fi

  文法3:

    if 條件

    then

      條件序列

    elif 條件

    then

      條件序列

      elif 條件

    then

      條件序列

    else

      條件序列

    fi

例子

#!/bin/bashif  [ -d /tmp/123 ];thenecho "this is directory"else echo "this is not directory"fi

  

case語句

  文法1:  

    case $變數名稱  in

    條件1)

      命令序列

      ;;

    條件2)

      命令序列

      ;;

    條件3)

      命令序列

      ;;

    *)

      命令序列

    esac

  文法2:

    case $變數名稱 in 

    條件1|條件4)

      命令序列

      ;;

    條件2|條件5)

      命令序列

      ;;

    條件3|條件6)

      命令序列

      ;;

      *)

      命令序列

    esac

 1 #!/bin/bash 2 case $1 in  3 top) 4     top 5     ;; 6 free) 7     free 8     ;; 9 df)10     df11      ;;12 *)13     echo "no param"14 esac
View Code

 

函數定義:

  文法1:

  方法名(){

    命令序列    

  }

  文法2:

  function 方法名{

    命令序列

  }

1 #!/bin/bash2 sum(){3   echo $(($1+$2))4 }5 sum 5 6

 

聯繫我們

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