UNIX Shell控制結構—IF

來源:互聯網
上載者:User

UNIX Shell控制結構—IF 流量控制(Decision Making)IF語句有三種格式:第一種:if ... fi statement  www.2cto.com  下面是一個執行個體:[plain] cat if1.sh  #!/bin/sh  a=10  b=20  #①  if [ $a -eq $b ]; then    echo "a is equal to b";  fi  if [ $a -gt $b ]; then    echo "a is great than b";  fi  #②  if [ $a -lt  $b ]  then    echo "a is less than b";  fi  # the EOF  注意:  www.2cto.com  ①條件和處理命令分開的一種寫法:if 條件; then處理命令fi②條件和處理命令分開的另一種寫法:if 條件then處理命令fi這裡需要根據個人習慣去選擇。 上面的例子中,變數的值是賦死了的,若要給此指令碼傳遞兩個參數,可做如下修改:[plain] cat if1.sh  #!/bin/sh  # a=10  # b=20  if [ $1 -eq $2 ]; then    echo "the first number is equal to the second";  fi  if [ $1 -gt $2 ]; then    echo "the first number is great than the second";  fi  if [ $1 -lt  $2 ]  then    echo "the first number is less than the second";  fi  # the EOF   給指令碼傳遞參數,只需要在sh命令後面添加即可,使用空格隔開:[plain] sh if1.sh 1 2  the first number is less than the second  sh if1.sh 12 1  the first number is great than the second  sh if1.sh 1 1  the first number is equal to the second   第二種:if ... else ... fi,具體如下:if [ expression ]then    statement(s) to be sxecuted if expression is trueelse    statement(s) to be sxecuted if expression is not truefi 一個簡單的執行個體[plain] cat ifparam.sh  #!/bin/sh  if [ $# -lt 3 ]; then     echo "Usage:`basename $0` arg1 arg2 arg3" >&2     exit 1  fi  #EOF  echo "arg1:$1"echo "arg1:$2"echo "arg1:$3"指令碼註解:1.$#表示參數輸入的個數2.basename $0列印檔案的名稱3.若輸入小於三個參數則,將輸出一個資訊,這個資訊被當做是錯誤資訊(>&2)。執行指令碼:sh ifparam.sh scott tomUsage:ifparam.sh arg1 arg2 arg3 sh ifparam.sh scott tom jimarg1:scottarg1:tomarg1:jim 再來看一個測試:[plain] cat ifeditor.sh  #!/bin/csh  #if [ -z $EDITOR ]①  #if [ -z "`echo $EDITOR`" ]③  #下面這種寫法不能正確的計算出環境的值  #因為wc -c計算包括新的空的行  #if [ `echo $EDITOR | wc -c` -eq 0 ]②  if [ -z "`echo $EDITOR`" ]   then     echo "Your EDITOR environment is not set"  else     echo "Using $EDITOR as the default editor"  fi  #EOF  sh ifeditor.shYour EDITOR environment is not set這裡使用三種方式去檢測環境變數是否設定;①直接報錯:語法錯誤②正確寫法,使用test檢測,-z表示如果為設定則長度為0傳回值為true③wc -c計算包括了空行,所以不準確 第三種:if ... elif ... fi,具體如下:[plain] if [ expression 1 ]; then     statement(s) to be sxecuted if expression 1 is true  elif [ expression 2 ]; then     statement(s) to be sxecuted if expression 2 is true  elif [ expression 3 ]; then     statement(s) to be sxecuted if expression 3 is true  else     statement(s) to be sxecuted if no expression is true  fi  下面是一個比較兩個數字大小的例子:[plain] cat elif.sh  #!/bin/sh  if [ $1 == $2 ]; then     echo "the first number is equal to the next"  elif [ $1 -gt $2 ]; then     echo "the first number is great than the next"  elif [ $1 -lt $2 ]; then     echo "the first number is great than the next"  else     echo "None of the condition met"  fi  #EOF  當不輸入任何數位時候,也就是為空白,結果如下:sh elif.shthe first number is equal to the next當輸入數位時候,如下:sh elif.sh 10 20elif.sh: test: unknown operator ==這種情況,我們可以將其錯誤資訊輸入到一個檔案當中,如下:sh elif.sh 10 20 > log.txt 2>&1cat log.txtelif.sh: test: unknown operator ==當將“==”號修改為-eq,結果如下:sh elif.sh 10 20the first number is less than the next 下面是一個if的執行個體,包括這三種命令格式;指令碼的作用是建立一個目錄,如果不輸入任何值,則列印指令碼的作用說明;輸入則提示是否建立,輸入非提示,則報錯誤,否則按提示走。[plain] #!/bin/sh  DIR=$1  if [ "$DIR" = "" ]; then     echo "Usage:`basename $0` directory to create" >&2     exit 1  fi      if [ -d $DIR ]; then     echo "Directory $DIR exists"  else     echo "The Directory does exist"     echo -n "Create it now?[y..n]:"     read ANS     if [ "$ANS" = "y" ] || [ "$ANS" = "Y" ]; then        echo "creating now"        mkdir $DIR > log.txt 2>&1        if [ $? -ne 0 ]; then           echo "Errors creating the directory $DIR" >&2           exit 1        fi        echo "Creating successful"     elif [ "$ANS" = "n" ] || [ "$ANS" = "N" ]; then        echo "Giving up creating directory $DIR"     else        echo "Bad input"     fi  fi  #EOF  1.不輸入參數sh ifmkdir.shUsage:ifmkdir.sh directory to create2.輸入一個存在的目錄,提示目錄已經存在:sh ifmkdir.sh testDirectory test exists查看確實有test目錄:[ -d test ]echo $?0建立test1目錄:sh ifmkdir.sh test1The Directory does existCreate it now?[y..n]:ycreating nowCreating successful3.執行指令碼,但不想建立目錄:sh ifmkdir.sh test2The Directory does existCreate it now?[y..n]:nGiving up creating directory test24.執行指令碼時,不按照提示輸入:sh ifmkdir.sh test2The Directory does existCreate it now?[y..n]:dBad input

聯繫我們

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