<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"  
    主站蜘蛛池模板: 久久精品7亚洲午夜a| 国产大片91精品免费看3| 亚洲精品乱码久久久久久蜜桃不卡| 亚洲AV日韩AV一区二区三曲| 国产成人精品免费视频网页大全| 久久亚洲日韩精品一区二区三区| 99精品视频在线观看免费| 亚洲中文无韩国r级电影| 七次郎成人免费线路视频| 亚洲精品第一国产综合境外资源| 男男黄GAY片免费网站WWW| 亚洲成年人啊啊aa在线观看| 污网站在线免费观看| 亚洲自偷自偷偷色无码中文| 最新国产乱人伦偷精品免费网站| 亚洲av无码乱码国产精品| 91香蕉在线观看免费高清| 亚洲国产精品日韩在线观看| 毛片免费视频观看| 日本系列1页亚洲系列| 亚洲成a人片在线观看久| 免费看无码特级毛片| 久久精品国产亚洲AV大全| 在线a人片天堂免费观看高清| AV激情亚洲男人的天堂国语| 日韩精品电影一区亚洲| 最近免费字幕中文大全| 亚洲成人黄色网址| 国产成人涩涩涩视频在线观看免费| 人人爽人人爽人人片av免费 | 内射无码专区久久亚洲| 四虎国产精品免费永久在线| 亚洲小说区图片区| 免费一级毛片不卡不收费| 精品一卡2卡三卡4卡免费视频| 国产成人精品日本亚洲直接| 亚洲欧洲自拍拍偷精品 美利坚| 美女被cao网站免费看在线看| 亚洲欧美国产精品专区久久| 亚洲精品白浆高清久久久久久| 嫖丰满老熟妇AAAA片免费看|