shell筆記之控制結構

來源:互聯網
上載者:User


shell筆記之控制結構 if 語句:Shell代碼  #!/bin/bash  www.2cto.com  # if 語句格式:  # if 和then放在不同行,並使else和結束處必須的fi與它們水平對齊  if [ "${1}" = '' ]  then      echo no argument input .  else      echo first argument is : $1  fi    var=2  echo "${var}"    if [ "${var}" = "1" ]  then       echo one .  elif [ "${var}" = "2" ]  then      echo two .  elif [ "${var}" = "3" ]  then      echo three .  else      echo at least four .  fi   for迴圈:Shell代碼  #!/bin/bash    # in 關鍵字後面是字列表,  for x in one two three four  do      echo number $x  done    for var in "$@"  do      echo you pass in $var  done    # 使用檔案萬用字元  for myfile in /etc/r*  do      if [ -d "$myfile" ]      then          echo "$myfile is dir"      else          echo "$myfile"      fi  done    # 還可以在字列表中使用多個萬用字元,甚至是變數  # 可以使用相對路徑或絕對路徑;對於相對路徑,bash是相對於當前工作目錄執行萬用字元擴充。    www.2cto.com  for x in ./*  var/lo* /home/${USER}/*  do      echo $x  done   while和until 迴圈:Shell代碼  #!/bin/bash    echo use while loop control .  myvar=0  while [ $myvar -ne 10 ]  do      echo $myvar      myvar=$(( $myvar + 1 ))  done    www.2cto.com  echo    echo    echo use until loop control .  myvar=10  until [ $myvar -eq 0 ]  do      echo $myvar      myvar=$(( $myvar - 1 ))  done  

聯繫我們

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