無線&移動互聯網技術研發

          換位思考·····
          posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          控制流結構——if then else

          Posted on 2009-11-29 12:05 Gavin.lee 閱讀(660) 評論(0)  編輯  收藏 所屬分類: Linux shell 入門

           

          if then else語句
          Shell代碼
          1. If  條件1    //如果條件1為真   
          2. Then         //那么   
          3. 命令1       //執行命令1  
          4. elif  條件2    //如果條件1不成立   
          5. then           //那么   
          6. 命令2        //執行命令2  
          7. else           //如果條件12均不成立   
          8. 命令3       //那么執行命令3  
          9. fi             //完成  



          簡單的if語句
          最普通的if語句是:
          if條件
          then  命令
          if
          使用if語句時,必須將then部分放在新行,否則會產生錯誤。如果要不分行,必須使用命令分隔符。本

          書其余部分將采取這種形式。現在簡單 if語句變為:
          if  條件;then
          命令
          if

          Shell代碼
          1. /home/l/g/tomotoboy >cat iftest   
          2. #!/bin/sh   
          3. #iftest   
          4. #this is a comment line,all comment lines start with a#   
          5. if [ "12" -lt "14" ]   
          6. then   
          7.  #yes 12 is less than 14  
          8.  echo  "Yes, 12 is less than 14"  
          9. fi   
          10.   
          11. /home/l/g/tomotoboy >chmod u+x iftest   
          12. /home/l/g/tomotoboy > ./iftest   
          13. Yes, 12 is less than 14  
          變量值測試
          不必拘泥于變量或數值測試,也可以測知系統命令是否成功返回。對 grep使用if語句找出grep是否成功

          返回信息。下面的例子中 grep用于查看tomotoboy是否在數據文件sed.txt中,注意'tomotoboy'用于精

          確匹配。
          Shell代碼
          1. /home/l/g/tomotoboy >cat grepif   
          2. #!/bin/sh   
          3. #grep if   
          4. if grep 'tomotoboy' sed.txt >/dev/null 2>&1  
          5. then   
          6.  echo "tomotoboy is  in the file"  
          7. else   
          8.  echo "tomotoboy is not in the file"  
          9. fi   
          10.   
          11. /home/l/g/tomotoboy >./grepif   
          12. tomotoboy is  in the file  

          用變量測試grep輸出
          正像前面看到的,可以用grep作字符串操作。下面的腳本中,用戶輸入一個名字列表,grep在變量中查找,要求其查找指定字符串

          Shell代碼
          1. /home/l/g/tomotoboy >cat grepstr   
          2. #!/bin/sh   
          3. #grepstr   
          4. echo -n "Enter a piece of text file:"  
          5. read TEXT   
          6. echo -n "Enter a string to query: "  
          7. read QUERY   
          8. if grep $QUERY $TEXT >/dev/null 2>&1  
          9. then   
          10.    echo "$QUERY is in $TEXT"  
          11.    #could do some processing here...   
          12. else   
          13.    echo "$QUERY is not in  $TEXT"  
          14. fi   
          15.   
          16. /home/l/g/tomotoboy >./grepstr   
          17. -n Enter a piece of text file:   
          18. sed.txt   
          19. -n Enter a string to query:   
          20. tomotoboy   
          21. tomotoboy is in sed.txt  

          文件拷貝輸出檢查
          下面測試文件拷貝是否正常,如果 cp命令并沒有拷貝文件myfile到myfile.bak,則打印錯誤信息。注意錯誤信息中` basename $0`打印腳本名。如果腳本錯誤退出,一個好習慣是顯示腳本名并將之定向到標準錯誤中。用戶應該知道產生錯誤的腳本名。
          Shell代碼
          1. /home/l/g/tomotoboy >chmod u+x ifcp   
          2. /home/l/g/tomotoboy >ifcp   
          3. cp: cannot access myfile   
          4. ifcp: error could not copy the file   
          5. /home/l/g/tomotoboy >cat ifcp   
          6. #!/bin/sh   
          7. #ifcp   
          8. if cp myfile myfile.bak; then   
          9.  echo "good copy"  
          10. else   
          11.  echo "`basename $0`: error could not copy the file" >&2  
          12. fi   
          13. /home/l/g/tomotoboy >touch myfile   
          14. /home/l/g/tomotoboy >ifcp   
          15. good copy   
          16. /home/l/g/tomotoboy >  


          當前目錄測試
          當運行一些管理腳本時,可能要在根目錄下運行它,特別是移動某種全局文件或進行權限改變時。一個簡單的測試可以獲知是否運行在根目錄下。下面腳本中變量DIRECTORY使用當前目錄的命令替換操作,然后此變量值與 " / "字符串比較(/為根目錄) 。如果變量值與字符串不等,則用戶退出腳本,退出狀態為1意味錯誤信息產生。
          Shell代碼
          1. /home/l/g/tomotoboy >ifpwd   
          2. You need to be in the root directory not /home/l/g/tomotoboy to run this script   
          3. /home/l/g/tomotoboy >cd /etc   
          4. /etc >cd /   
          5. / >/home/l/g/tomotoboy/ifpwd   
          6. / >cat ifpwd   
          7. cat: cannot open ifpwd   
          8. / >cat /home/l/g/tomotoboy/ifpwd   
          9. #!/bin/sh   
          10. #ifpwd   
          11. DIRECTORY=`pwd`   
          12. #grab the current directory   
          13. if [ "$DIRECTORY" != "/" ];then   
          14. #is it the root directory?   
          15. #no ,the direct output to standard  error,which is the screen by default.   
          16. echo "You need to be in the root directory not $DIRECTORY to run this script" >&2  
          17. #exit with a value of 1, an error   
          18. exit 1  
          19. fi  


          文件權限測試
          可以用i f語句測試文件權限,下面簡單測試文件sed.txt是否可寫
          Shell代碼
          1. /home/l/g/tomotoboy >ifwr sed.txt   
          2. You can write to sed.txt   
          3. /home/l/g/tomotoboy >cat ifwr   
          4. #!/bin/sh   
          5. #ifwr   
          6. if [ ! -w "$1" ]; then   
          7.   echo "You cannot write to $1" >&2  
          8. else   
          9.   echo "You can write to $1"  
          10. fi  


          測試傳遞到腳本中的參數
          if語句可用來測試傳入腳本中參數的個數。使用特定變量$#,表示調用參數的個數。可以測試所需參數個數與調用參數個數是否相等。以下測試確保腳本有三個參數。如果沒有,則返回一個可用信息到標準錯誤,然后代碼退出并顯示退出狀態。如果參數數目等于3,則顯示所有參數。
          Shell代碼
          1. /home/l/g/tomotoboy >cat ifparam   
          2. #!/bin/sh   
          3. #ifparam   
          4. if [ $# -lt 3 ]; then   
          5. #less than 3 parameters called,echo  a usage message and exit   
          6. echo "Usage: `basename $0` arg1 arg2 arg3" >&2  
          7. exit   
          8. fi   
          9. #good ,receive 3 params, let's echo them   
          10. echo "arg1: $1"  
          11. echo "arg2: $2"  
          12. echo "arg3: $3"  
          13. /home/l/g/tomotoboy >ifparam yang shi hai   
          14. arg1: yang   
          15. arg2: shi   
          16. arg3: hai  


          決定腳本是否為交互模式
          有時需要知道腳本運行是交互模式(終端模式)還是非交互模式(cron或at) 。腳本也許需要這個信息以決定從哪里取得輸入以及輸出到哪里,使用test命令并帶有-t選項很容易確認這一點。如果test返回值為1,則為交互模式。
          Shell代碼
          1. /home/l/g/tomotoboy >cat ifinteractive   
          2. #!/bin/sh   
          3. #ifinteractive   
          4. if [ -t ];then   
          5. echo "We are interactive with a terminal"  
          6. else   
          7. echo "We must be running from some background process probably cron or at"  
          8. fi   
          9. /home/l/g/tomotoboy >ifinteractive   
          10. We are interactive with a terminal  

          簡單的if else語句
          下一個if語句有可能是使用最廣泛的:
          Shell代碼
          1. if條件   
          2. then   
          3. 命令1  
          4. else   
          5. 命令2  
          6. fi  
          使用if語句的else部分可在條件測試為假時采取適當動作。
          變量設置測試,下面的例子測試環境變量EDITOR是否已設置。如果EDITOR變量為空,將此信息通知用戶。如果已設置,在屏幕上顯示編輯類型。
          Shell代碼
          1. /home/l/g/tomotoboy >echo $EDITOR   
          2.   
          3. /home/l/g/tomotoboy >cat ifeditor   
          4. #!/bin/sh   
          5. #ifeditor   
          6. if [ -z "$EDITOR" ];then   
          7. #the variable has not been set   
          8. echo "Your EDITOR environment is not set"  
          9. else   
          10. #let's  us see what is it   
          11. echo "Using $EDITOR as the default editor"  
          12. fi   
          13. /home/l/g/tomotoboy >ifeditor   
          14. Your EDITOR environment is not set   
          15. /home/l/g/tomotoboy >  


          檢測最后命令狀態
          前面將目錄名傳入腳本創建了一個目錄,腳本然后提示用戶是否應創建目錄。下面的例子創建一個目錄,并從當前目錄將所有 *.txt文件拷入新目錄。但是這段腳本中用最后狀態命令檢測了每一個腳本是否成功執行。如果命令失敗則通知用戶。
          Shell代碼
          1. /home/l/g/tomotoboy >cat ifmkdir   
          2. #!/bin/sh   
          3. #ifmkdir   
          4. DIR_NAME=testdirec   
          5. #where we are?   
          6. THERE=`pwd`   
          7. #send all output to system dustbin   
          8. mkdir $DIR_NAME >/dev/null 2>&1  
          9. #is it a directory?   
          10. if [ -d $DIR_NAME ];then   
          11. #can we cd to the directory   
          12.   cd $DIR_NAME   
          13.   if [ $? = 0 ];then   
          14.   #yes we can   
          15.   HERE=`pwd`   
          16.   echo "$HERE"  
          17.   cp $THERE/*.txt $HERE   
          18.   else   
          19.    echo "Cannot cd to $DIR_NAME" >&2  
          20.    exit 1  
          21.   fi   
          22. else   
          23.   echo "Cannot create directory $DIR_NAME" >&2  
          24.   exit 1  
          25. fi  

          簡單的安全登錄腳本
          以下是用戶登錄時啟動應用前加入相應安全限制功能的基本框架。首先提示輸入用戶名和密碼,如果用戶名和密碼均匹配腳本中相應字符串,用戶登錄成功,否則用戶退出。腳本首先設置變量為假—總是假定用戶輸入錯誤,stty當前設置被保存,以便隱藏passwd域中字符,然后重新保存stty設置。如果用戶I D和密碼正確(密碼是myday) ,明亮INVALID_USER和INVALID_PASSWD設置為no表示有效用戶或密碼,然后執行測試,如果兩個變量其中之一為yes,缺省情況下,腳本退出用戶。鍵入有效的ID和密碼,用戶將允許進入。這是一種登錄腳本的基本框架。下面的例子中有效用戶ID為dave或tomotoboy。
          Shell代碼
          1. #!/bin/sh   
          2. #ifpass   
          3. #set the variables to false   
          4. INVALID_USER=yes   
          5. INVALID_PASSWD=yes   
          6. #set the current stty settings   
          7. SAVEDSTTY=`stty -g`   
          8. echo "You are logging into a sensitive area"  
          9. echo -n "Enter your ID name:"  
          10. read NAME   
          11. #hide the characters typed in   
          12. stty -echo   
          13. echo "Enter your password :"  
          14. read PASSWORD   
          15. #back on again   
          16. stty $SAVEDSTTY   
          17. if [ "$NAME" = "tomotoboy" ] || [ "$NAME" = "dave" ]; then   
          18.  #if a valid then set variable   
          19.     INVALID_USER=no   
          20. fi   
          21. if [ "$PASSWORD" = "myday" ];then   
          22.  #if valid password then set variable   
          23.     INVALID_PASSWD=no   
          24. fi   
          25. if [ "$INVALID_USER" = "yes" ] || [ "$INVALID_PASSWD" = "yes" ];then   
          26.    echo "`basename $0` : Sorry wrong password or userid"  
          27.    exit 1  
          28. fi   
          29. echo "corrent user id and password given"  

          主站蜘蛛池模板: 新宁县| 沐川县| 绥芬河市| 金溪县| 阳高县| 城口县| 新疆| 云安县| 乐亭县| 泰宁县| 贡觉县| 伊吾县| 仪征市| 星子县| 新化县| 娄底市| 监利县| 凤凰县| 霞浦县| 额尔古纳市| 镇宁| 南郑县| 枣阳市| 连山| 盐边县| 博野县| 绵竹市| 岳西县| 大洼县| 陇川县| 滨州市| 临西县| 桂林市| 塘沽区| 上虞市| 开平市| 阆中市| 庄浪县| 孝义市| 石狮市| 马鞍山市|