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

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

          日歷

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          我參與的團隊

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 357886
          • 排名 - 155

          向腳本傳遞參數

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

           

          shift命令
          向腳本傳遞參數時,有時需要將每一個參數偏移以處理選項,這就是 shift命令的功能。它每次將參數位置向左偏移一位,下面用一段簡單腳本詳述其功能。腳本使用 while循環反饋所有傳遞到腳本的參數。使用shift命令來處理傳遞到腳本的每一個參數:
          Shell代碼
          1. #!/bin/sh   
          2. loop=0  
          3. while [ $# -ne 0 ]   
          4. do    
          5.    echo $1  
          6.    shift   
          7. done  

          使用shift處理文件大小寫轉換
          Shell代碼
          1. !#/bin/sh   
          2. # tr_case   
          3. # convert files to either  upper or lower case   
          4. FILES=""  
          5. TRCASE=""  
          6. EXT=""  
          7. OPT=no   
          8.   
          9. # gets called when a conversion fails   
          10. error_msg()   
          11. {   
          12. _FILENAME=$1  
          13. echo "`basename $0`: Error the conversion failed on $_FILENAME"  
          14. }   
          15.   
          16. if [ $# -eq 0 ]   
          17. then    
          18.    echo "For more info try `basename $0` --help"  
          19.    exit 1  
          20. fi   
          21.   
          22. while [ $# -gt 0 ]   
          23. do   
          24.   case $1 in   
          25.   #set the variables based on what option was used   
          26.   -u)TRCASE=upper   
          27.      EXT=".UC"  
          28.      OPT=yes   
          29.      shift   
          30.      ;;   
          31.   -l)TRCASE=lower   
          32.      EXT=".LC"  
          33.      OPT=yes   
          34.      shift   
          35.      ;;   
          36.   -help) echo "convert a file(s) to uppercase from lowercase"  
          37.          echo "convert a file(s) from lowercase to uppercase"  
          38.          echo "will convert all characters according to the sepcified command option."  
          39.          echo "where option is"  
          40.          echo "-l Convert to lowercase"  
          41.          echo "-u Convert to uppercase"  
          42.          echo "The original file(s) is not touched. A new file(s)"  
          43.          echo "will be created with either a .UC or .LC extension"  
          44.          echo "usage: $0 -[l|u] file [file..]"  
          45.      exit 0  
          46.      ;;   
          47.     -*) echo "usage: `basename $0` -[l|u] file [file..]"  
          48.      exit 1  
          49.      ;;   
          50.   
          51.      *) # collect the files to process   
          52.      if [ -f $1 ]   
          53.        then   
          54.            # add the filenames to a variable list   
          55.            FILES=$FILES" "$1  
          56.        else   
          57.            echo "`basename $0` : Error cannot find the file $1"  
          58.      fi   
          59.      shift   
          60.      ;;   
          61.    esac   
          62. done   
          63.   
          64. if [ "$OPT" = "no" ]   
          65. then   
          66.    echo "`basename $0`: Error you need to specify an option. No action taken"  
          67.    echo "`basename` --help"  
          68.    exit 1  
          69. fi   
          70.              
          71. # now read in all the file(s)   
          72. # use the variable LOOP, I just love the word LOOP   
          73. for LOOP in $FILES   
          74. do   
          75.  case $TRCASE in   
          76.  lower) cat $LOOP|tr "[a-z]" "[A-Z]" >$LOOP$EXT   
          77.     if [ $? != 0 ]   
          78.        then    
          79.        error_msg $LOOP   
          80.     else   
          81.        echo "Converted file called $LOOP$EXT"  
          82.     fi   
          83.     ;;   
          84.  upper) cat $LOOP|tr "[A-Z]" "[a-z]" > $LOOP$EXT   
          85.     if [ $? != 0 ]   
          86.     then   
          87.        error_msg $LOOP   
          88.     else   
          89.        echo "Converted file called $LOOP$EXT"  
          90.     fi   
          91.     ;;   
          92.   esac   
          93. done  

          getopts命令
          Shell代碼
          1. #!/bin/sh   
          2. #getopts1   
          3. #set the vars   
          4. ALL=false   
          5. HELP=false   
          6. FILE=false   
          7. VERBROSE=false   
          8.   
          9. while getopts ahfgv OPTION   
          10. do   
          11.   case $OPTION in   
          12.   a)ALL=true   
          13.     echo "ALL is $ALL"  
          14.     ;;   
          15.   h)HELP=true   
          16.     echo "HELP is $HELP"  
          17.     ;;   
          18.   f)FILE=true   
          19.     echo "FILE is $FILE"  
          20.     ;;   
          21.   v)VERBOSE=true   
          22.     echo "VERROSE is $VERROSE"  
          23.     ;;   
          24.   esac   
          25. done  

          Shell代碼
          1. /home/l/g/tomotoboy/getopts >getopts1 -a   
          2. ALL is true   
          3. /home/l/g/tomotoboy/getopts >getopts1 -v   
          4. VERROSE is   
          5. /home/l/g/tomotoboy/getopts >getopts1 -v -a -h   
          6. VERROSE is   
          7. ALL is true   
          8. HELP is true  
          getopts一般格式為:
          getopts  option_string variable
          在上述例子中使用腳本:
          while getopts ahfgv OPTION
          可以看出while循環用于讀取命令行,option_string為指定的5個選項(- a,- h,- f,- g,- v),腳本中variable為OPTION。注意這里并沒有用連字符指定每一單個選項。

          使用getopts指定變量取值
          有時有必要在腳本中指定命令行選項取值。getopts為此提供了一種方式,即在option_string中將一個冒號放在選項后。例如:
          getopts ahfvc: OPTION
          上面一行腳本指出,選項 a、h、f、v可以不加實際值進行傳遞,而選項 c必須取值。使用選項取值時,必須使用變量 OPTARG保存該值。如果試圖不取值傳遞此選項,會返回一個錯誤信息。錯誤信息提示并不明確,因此可以用自己的反饋信息屏蔽它,方法如下:
          將冒號放在option_string開始部分。
          Shell代碼
          1. while getopts :ahfgvc:  OPTION  
          Shell代碼
          1. #!/bin/sh   
          2. #getopts1   
          3. #set the vars   
          4. ALL=false   
          5. HELP=false   
          6. FILE=false   
          7. VERBROSE=false   
          8. COPIES=0 #the value for the -c option is set to zero   
          9. while getopts :ahfgvc: OPTION   
          10. do   
          11.   case $OPTION in   
          12.   a)ALL=true   
          13.     echo "ALL is $ALL"  
          14.     ;;   
          15.   h)HELP=true   
          16.     echo "HELP is $HELP"  
          17.     ;;   
          18.   f)FILE=true   
          19.     echo "FILE is $FILE"  
          20.     ;;   
          21.   v)VERBOSE=true   
          22.     echo "VERROSE is $VERROSE"  
          23.     ;;   
          24.   c)COPIES=$OPTARG   
          25.     echo "COPIES is $COPIES"  
          26.     ;;   
          27.   \?) #usage stagement   
          28.     echo "`basename $0` -[a h f v] -[c value] file" >&2  
          29.     ;;   
          30.  esac   
          31. done    


          Shell代碼
          1. /home/l/g/tomotoboy/getopts >chmod 755 getopts2   
          2. /home/l/g/tomotoboy/getopts >getopts2 -c hello   
          3. COPIES is hello   
          4. /home/l/g/tomotoboy/getopts >getopts2 -h -c hello   
          5. HELP is true   
          6. COPIES is hello  


          訪問取值方式
          getopts的一種功能是運行后臺腳本。這樣可以使用戶加入選項,指定不同的磁帶設備以備份數據。使用getopts實現此任務的基本框架如下:
          Shell代碼
          1. #!/bin/sh   
          2. QUITE=n   
          3. DEVICE=awa   
          4. LOGFILE=/tmp/logbackup   
          5. usage()   
          6. {   
          7.  echo "Usage: `basename $0` -d [device] -l [logfile] -q"  
          8.  exit 1  
          9.   
          10. }   
          11. if [ $# = 0 ]   
          12. then    
          13.   usage   
          14. fi   
          15.   
          16. while getopts :qd:l: OPTION   
          17. do   
          18.   case $OPTION in   
          19.   q) QUIET=y   
          20.      LOGFILE="/tmp/backup.log"  
          21.      ;;   
          22.   d) DEVICE=$OPTARG   
          23.      ;;   
          24.   l) LOGFILE=$OPTARG      
          25.      ;;   
          26.   \?)usage   
          27.      ;;   
          28.    esac   
          29. done   
          30. echo "you chose the following options...I can now process these"  
          31. echo "Quite = $QUIET $DEVICE $LOGFILE"  
          主站蜘蛛池模板: 南城县| 丹阳市| 正镶白旗| 丽江市| 岳普湖县| 台北市| 周宁县| 阳城县| 沐川县| 闻喜县| 铜川市| 买车| 教育| 西吉县| 曲麻莱县| 新晃| 瓮安县| 灵山县| 醴陵市| 延寿县| 秭归县| 依兰县| 汉阴县| 江陵县| 凌海市| 山丹县| 岳池县| 临泉县| 富蕴县| 临沭县| 温泉县| 通许县| 五家渠市| 潮州市| 宜川县| 岳普湖县| 桐梓县| 资中县| 白沙| 邯郸市| 二连浩特市|