无线&移动互联网技术研发

换位思考·····
posts - 19, comments - 53, trackbacks - 0, articles - 283
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

向脚本传递参数

Posted on 2009-11-29 12:10 Gavin.lee 阅读(355) 评论(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"  

只有注册用户登录后才能发表评论。


网站导航: