posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          控制流結(jié)構(gòu)——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       //執(zhí)行命令1  
          4. elif  條件2    //如果條件1不成立   
          5. then           //那么   
          6. 命令2        //執(zhí)行命令2  
          7. else           //如果條件12均不成立   
          8. 命令3       //那么執(zhí)行命令3  
          9. fi             //完成  



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

          書其余部分將采取這種形式。現(xiàn)在簡單 if語句變?yōu)椋?
          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  
          變量值測試
          不必拘泥于變量或數(shù)值測試,也可以測知系統(tǒng)命令是否成功返回。對 grep使用if語句找出grep是否成功

          返回信息。下面的例子中 grep用于查看tomotoboy是否在數(shù)據(jù)文件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`打印腳本名。如果腳本錯誤退出,一個好習(xí)慣是顯示腳本名并將之定向到標(biāo)準(zhǔn)錯誤中。用戶應(yīng)該知道產(chǎn)生錯誤的腳本名。
          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 >  


          當(dāng)前目錄測試
          當(dāng)運(yùn)行一些管理腳本時,可能要在根目錄下運(yùn)行它,特別是移動某種全局文件或進(jìn)行權(quán)限改變時。一個簡單的測試可以獲知是否運(yùn)行在根目錄下。下面腳本中變量DIRECTORY使用當(dāng)前目錄的命令替換操作,然后此變量值與 " / "字符串比較(/為根目錄) 。如果變量值與字符串不等,則用戶退出腳本,退出狀態(tài)為1意味錯誤信息產(chǎn)生。
          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  


          文件權(quán)限測試
          可以用i f語句測試文件權(quán)限,下面簡單測試文件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  


          測試傳遞到腳本中的參數(shù)
          if語句可用來測試傳入腳本中參數(shù)的個數(shù)。使用特定變量$#,表示調(diào)用參數(shù)的個數(shù)。可以測試所需參數(shù)個數(shù)與調(diào)用參數(shù)個數(shù)是否相等。以下測試確保腳本有三個參數(shù)。如果沒有,則返回一個可用信息到標(biāo)準(zhǔn)錯誤,然后代碼退出并顯示退出狀態(tài)。如果參數(shù)數(shù)目等于3,則顯示所有參數(shù)。
          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  


          決定腳本是否為交互模式
          有時需要知道腳本運(yùn)行是交互模式(終端模式)還是非交互模式(cron或at) 。腳本也許需要這個信息以決定從哪里取得輸入以及輸出到哪里,使用test命令并帶有-t選項很容易確認(rèn)這一點。如果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部分可在條件測試為假時采取適當(dāng)動作。
          變量設(shè)置測試,下面的例子測試環(huán)境變量EDITOR是否已設(shè)置。如果EDITOR變量為空,將此信息通知用戶。如果已設(shè)置,在屏幕上顯示編輯類型。
          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 >  


          檢測最后命令狀態(tài)
          前面將目錄名傳入腳本創(chuàng)建了一個目錄,腳本然后提示用戶是否應(yīng)創(chuàng)建目錄。下面的例子創(chuàng)建一個目錄,并從當(dāng)前目錄將所有 *.txt文件拷入新目錄。但是這段腳本中用最后狀態(tài)命令檢測了每一個腳本是否成功執(zhí)行。如果命令失敗則通知用戶。
          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  

          簡單的安全登錄腳本
          以下是用戶登錄時啟動應(yīng)用前加入相應(yīng)安全限制功能的基本框架。首先提示輸入用戶名和密碼,如果用戶名和密碼均匹配腳本中相應(yīng)字符串,用戶登錄成功,否則用戶退出。腳本首先設(shè)置變量為假—總是假定用戶輸入錯誤,stty當(dāng)前設(shè)置被保存,以便隱藏passwd域中字符,然后重新保存stty設(shè)置。如果用戶I D和密碼正確(密碼是myday) ,明亮INVALID_USER和INVALID_PASSWD設(shè)置為no表示有效用戶或密碼,然后執(zhí)行測試,如果兩個變量其中之一為yes,缺省情況下,腳本退出用戶。鍵入有效的ID和密碼,用戶將允許進(jìn)入。這是一種登錄腳本的基本框架。下面的例子中有效用戶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"  

          主站蜘蛛池模板: 资兴市| 象山县| 山丹县| 灵石县| 尚义县| 札达县| 子洲县| 宁化县| 姚安县| 海林市| 大方县| 聂拉木县| 海口市| 兴隆县| 逊克县| 马关县| 洛隆县| 榆树市| 太仓市| 浮山县| 青岛市| 石渠县| 十堰市| 龙海市| 庆元县| 延寿县| 汪清县| 临夏县| 荔波县| 嵊州市| 保定市| 垫江县| 海安县| 定结县| 杭锦旗| 开原市| 莒南县| 蓬安县| 盐山县| 安图县| 五寨县|