歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> SHELL編程

UNIX Shell控制結構—IF

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 true else     statement(s) to be sxecuted if expression is not true fi   一個簡單的實例 [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 tom Usage:ifparam.sh arg1 arg2 arg3   sh ifparam.sh scott tom jim arg1:scott arg1:tom arg1: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.sh Your 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.sh the first number is equal to the next 當輸入數字的時候,如下: sh elif.sh 10 20 elif.sh: test: unknown operator == 這種情況,我們可以將其錯誤信息輸入到一個文件當中,如下: sh elif.sh 10 20 > log.txt 2>&1 cat log.txt elif.sh: test: unknown operator == 當將“==”號修改為-eq,結果如下: sh elif.sh 10 20 the 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.sh Usage:ifmkdir.sh directory to create 2.輸入一個存在的目錄,提示目錄已經存在: sh ifmkdir.sh test Directory test exists 查看確實有test目錄: [ -d test ] echo $? 0 創建test1目錄:sh ifmkdir.sh test1 The Directory does exist Create it now?[y..n]:y creating now Creating successful 3.執行腳本,但不想創建目錄: sh ifmkdir.sh test2 The Directory does exist Create it now?[y..n]:n Giving up creating directory test2 4.執行腳本時,不按照提示輸入: sh ifmkdir.sh test2 The Directory does exist Create it now?[y..n]:d Bad input
Copyright © Linux教程網 All Rights Reserved