<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

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

    向腳本傳遞參數

    Posted on 2009-11-29 12:10 Gavin.lee 閱讀(369) 評論(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"  
    主站蜘蛛池模板: 亚洲人成日本在线观看| 亚洲精品无码久久久久sm| 久久免费美女视频| 国产中文在线亚洲精品官网| 国产AV无码专区亚洲AV毛网站| 一区二区三区视频免费| 狼群影院在线观看免费观看直播 | 久青草视频97国内免费影视| 亚洲精品高清在线| jizz在线免费观看| 亚洲国产日韩在线视频| 日本中文字幕免费高清视频| 亚洲一区影音先锋色资源| 99re免费99re在线视频手机版| 亚洲国产美女精品久久久久| 国产va免费精品观看精品| 亚洲综合在线一区二区三区| 国产成人高清精品免费软件| 一级毛片在线免费播放| 国产AV无码专区亚洲AV男同| 亚洲毛片免费观看| 亚洲精品国产综合久久久久紧| 一级毛片直播亚洲| 国精产品一区一区三区免费视频| 亚洲情a成黄在线观看动漫尤物| 日本成年免费网站| 亚洲av日韩av永久在线观看 | 歪歪漫画在线观看官网免费阅读 | 一级成人毛片免费观看| 亚洲乱色熟女一区二区三区丝袜| 免费无遮挡无码永久视频| 亚洲av无码久久忘忧草| 免费女人18毛片a级毛片视频| a毛片免费全部播放完整成| 亚洲精品中文字幕麻豆| 国产18禁黄网站免费观看| 女人隐私秘视频黄www免费| 亚洲一区在线视频观看| 亚洲精品视频久久久| 99久久99热精品免费观看国产| 亚洲av无码成人精品区一本二本|