<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"  
    主站蜘蛛池模板: 免费观看91视频| 毛片在线全部免费观看| 免免费国产AAAAA片| 成人最新午夜免费视频| 免费人成在线观看网站视频 | 波多野结衣中文字幕免费视频 | 91成人在线免费观看| 狠狠色伊人亚洲综合成人| 久久国产免费观看精品| 亚洲午夜免费视频| 美女露隐私全部免费直播| 亚洲另类自拍丝袜第五页 | 亚洲一区二区三区日本久久九| 国产精品亚洲高清一区二区| 久久精品国产亚洲AV无码娇色| 羞羞视频免费网站入口| 亚洲伊人久久成综合人影院| 久久国产乱子伦精品免费强| 亚洲综合激情视频| 成人免费午夜视频| 亚洲精品国产啊女成拍色拍| 在线观看无码AV网站永久免费| 午夜小视频免费观看| 一区二区三区AV高清免费波多| 日韩精品福利片午夜免费观着| 丁香花免费高清视频完整版| 亚洲人成网站在线播放2019| 亚洲国产精品嫩草影院久久 | 亚洲爆乳无码专区| 一级午夜免费视频| 国产成人综合久久精品免费| 一级女性全黄生活片免费看| 亚洲精品午夜视频| 国产一区二区三区免费在线观看| 亚洲av成人综合网| 91免费人成网站在线观看18| 亚洲精品高清国产一久久| 午夜老司机免费视频| 久久国产亚洲精品麻豆| 性xxxxx免费视频播放| 亚洲福利电影在线观看|