* ./test.sh--a-long=args --b-long :长选项 我们先来看getopts,它不支持长选项。 getopts配合case来进行操作时有两个隐含变量:一个是OPTARG,用来取当前选项的值,另外一个是OPTIND,代表当前选项在参数列表中的位移。OPTIND是一个特殊的变量,它的初始值是1,每次getopts处理完一个命令参数后就递增它,得到getopts要处理的下一个参数。
getopts 命令是 Korn/POSIX shell 的内置命令,用来从参数列表检索选项以及选项参数。选项由一个 (加号)或者是由一个-(减号)后跟一个字符开始。一个既不是以 ,也不是以-开始的选项结束选项字符串。每次调用 getopts 命令时,它将下一个选项的值放置在名称 内,并将下一个要处理的参数的索引置于 shell 变量 OPTIND 中。一旦调用了 shell , OPTIND 将初始化为1。当选项以 开头,则 将预先设为名称中的值。
如果选项字符串中的字符后面带有“:”(冒号),则预期此选项将带有参数。当选项需要选项参数时,getopts 命令就将其置于变量 OPTARG 中。 当查找到 选项字符串所不包含的选项字符,或者查找到的选项没有所需的选项参数时: -
- 如果选项字符串不以:(冒号)开头,
-
- 名称 将会被设置为 ?(问号)字符,
- OPTARG. 将被取消设置,并且
- 诊断消息将被写入到标准错误中。
这种情况被认为是在将参数传递给所调用的应用程序的过程中所检测到的错误,而不是在处理 getopts 命令的过程中所发生的错误;如上所述,写入诊断消息,但退出状态将变为零。 -
- 如果选项字符串以 :(冒号)开头,
-
- 名称 将被设为 ? (问号)字符,这是对未知的选项来说的,或者为缺少的所需选项设为:(冒号)字符,
- OPTARG 将被设置为已查找到的选项字符,并且
- 标准错误中将不写入任何输出。
以下任何字符都可以识别选项结尾:特殊选项- -,查找到不以-,或者 为开头的参数,或者遇到错误。 当遇到选项结尾时: -
- getopts 命令将退出运行,并且返回值大于零,
- OPTARG 将被设置为第一个非选项参数索引,在这种情况下,如果第一个 - - 参数之前未出现其它非选项参数,将认为它是选项参数,或者如果没有非选项参数,设置为值 $# 1, 名称将被设置为? (问号)字符,
使用getopts非常简单: 代码 #! /bin/shwhile getopts a:bc argdo case $arg in "a") echo "a: $OPTARG ($OPTIND)" ;; "b") echo "bb ($OPTIND)" ;; "c") echo "ccc ($OPTIND)" ;; ?) echo "unkonw argument ($OPTIND)" ;; esacdone
现在就可以使用: $ ./getopt.sh -a 111 -bc -da: 111 (3)bb (4)ccc (4)Illegal option -dunkonw argument (5)$ ./getopt.sh -a 111 -b -c -da: 111 (3)bb (4)ccc (5)Illegal option -dunkonw argument (6)
应该说绝大多数脚本使用该函数就可以了,如果需要支持长选项以及可选参数,那么就需要使用getopt. 下面是getopt自带的一个例子: #!/bin/bash # A small example program for using the new getopt(1) program. # This program will only work with bash(1) # An similar program using the tcsh(1) script language can be found # as parse.tcsh # Example input and output (from the bash prompt): # ./parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long " # Option a # Option c, no argument # Option c, argument `more' # Option b, argument ` very long ' # Remaining arguments: # --> `par1' # --> `another arg' # --> `wow!*\?' # Note that we use `"$@"' to let each command-line parameter expand to a # separate word. The quotes around `$@' are essential! # We need TEMP as the `eval set --' would nuke the return value of getopt. #-o表示短选项,两个冒号表示该选项有一个可选参数,可选参数必须紧贴选项 #如-carg 而不能是-c arg #--long表示长选项 #"$@"在上面解释过 # -n:出错时的信息 # -- :举一个例子比较好理解: #我们要创建一个名字为 "-f"的目录你会怎么办? # mkdir -f #不成功,因为-f会被mkdir当作选项来解析,这时就可以使用 # mkdir -- -f 这样-f就不会被作为选项。 TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \ -n 'example.bash' -- "$@"` if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi # Note the quotes around `$TEMP': they are essential! #set 会重新排列参数的顺序,也就是改变$1,$2...$n的值,这些值在getopt中重新排列过了 eval set -- "$TEMP" #经过getopt的处理,下面处理具体选项。 while true ; do case "$1" in -a|--a-long) echo "Option a" ; shift ;; -b|--b-long) echo "Option b, argument \`$2'" ; shift 2 ;; -c|--c-long) # c has an optional argument. As we are in quoted mode, # an empty parameter will be generated if its optional # argument is not found. case "$2" in "") echo "Option c, no argument"; shift 2 ;; *) echo "Option c, argument \`$2'" ; shift 2 ;; esac ;; --) shift ; break ;; *) echo "Internal error!" ; exit 1 ;; esac done echo "Remaining arguments:" for arg do echo '--> '"\`$arg'" ; done
比如我们使用 ./test -a -b arg arg1 -c 你可以看到,命令行中多了个arg1参数,在经过getopt和set之后,命令行会变为: -a -b arg -c -- arg1 $1指向-a,$2指向-b,$3指向arg,$4指向-c,$5指向--,而多出的arg1则被放到了最后。 3.总结
一般小脚本手工处理也许就够了,getopts能处理绝大多数的情况,getopt较复杂,功能也更强大。
有问题请指出,不胜感激。 转载地址:http://plwub.baihongyu.com/