您的位置:首页 > 运维架构 > Shell

shell 中 getopts 用法

2016-07-22 14:27 525 查看
在写sh脚本的时候,常常需要运行时输入一些数据。之前已经知道用基本的$*,执行的情况,大概就是$0 $1 $2 $3……
那么,那些系统命令里的参数又是怎么做出来的呢?我们自己的脚本如何搞出来$0
-$1的效果呢?这就是getopts的作用了。举例如下:
#!/bin/bash
echo "OPTIND starts at $OPTIND"
while getopts ":pq:" optname
do
case "$optname" in
"p")
echo "Option $optname is specified"
;;
"q")
echo "Option $optname has value $OPTARG"
;;
"?")
echo "Unknown option $OPTARG"
;;
":")
echo "No argument value for option $OPTARG"
;;
*)
# Should not occur
echo "Unknown error while processing options"
;;
esac
echo "OPTIND is now $OPTIND"
done
在使用getopts命令的时候,shell会自动产生两个变量OPTIND和OPTARG。
OPTIND初始值为1,其含义是下一个待处理的参数的索引。只要存在,getopts命令返回true,所以一般getopts命令使用while循环;并且OPTIND的体现并不明显。
OPTARG是当getopts获取到其期望的参数后存入的位置。而如果不在其期望内,则$optname被设为?并将该意外值存入OPTARG;如果$optname需要拥有具体设置值而实际却没有,则$optname被设为:并将丢失设置值的optname存入OPTARG;OPTARG就是选项后面跟的参数值,比如/usr/local/nginx/sbin/nginx -s stop 其中stop就是$OPTARG的值。
对于$optname,可以用后标:来表示是否需要值;而前标:则表示是否开启静默模式。
案例分析:
getopts optstring varname [arg ...]

optstring option字符串,会逐个匹配
varname 每次匹配成功的选项
arg 参数列表,没写时它会取命令行参数列表

$OPTIND 特殊变量,option index,会逐个递增
$OPTARG 特殊变量,option argument,不同情况下有不同的值

细则1:当optstring以”:“开头时,getopts会区分invalid option错误和miss option argument错误。
invalid option时,varname会被设成?,$OPTARG是出问题的option;
miss option argument时,varname会被设成:,$OPTARG是出问题的option。
如果optstring不以”:“开头,invalid option错误和miss option argument错误都会使
varname被设成?,$OPTARG是出问题的option。

细则2:当optstring中的字母跟”:“时(比如下面例子中的t),表明该option可接参数,参数(argument)放在$OPTARG中;
如果缺参数,且optstring是以”:“开头,则varname的值会是:,$OPTARG是该option,
否则varname的值是?,$OPTARG是该option。(参照细则1)
首先先来一个例子吧:
[cpp] view plain copy
[hello@Git shell]$ bash test.sh -a hello
this is -a the arg is ! hello
[hello@Git shell]$ more test.sh
#!/bin/bash

while getopts "a:" opt; do
case $opt in
a)
echo "this is -a the arg is ! $OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG"
;;
esac
done


上面的例子显示了执行的效果和代码。

getopts的使用形式是:getopts option_string variable getopts一共有两个参数,第一个是-a这样的选项,第二个参数是 hello这样的参数。选项之间可以通过冒号:进行分隔,也可以直接相连接,:表示选项后面必须带有参数,如果没有可以不加实际值进行传递例如:getopts ahfvc: option表明选项a、h、f、v可以不加实际值进行传递,而选项c必须取值。使用选项取值时,必须使用变量OPTARG保存该值。
[cpp] view plain copy
[hello@Git shell]$ bash test.sh -a hello -b
this is -a the arg is ! hello
test.sh: option requires an argument -- b
Invalid option: -
[hello@Git shell]$ bash test.sh -a hello -b hello -c
this is -a the arg is ! hello
this is -b the arg is ! hello
this is -c the arg is !
[hello@Git shell]$ more test.sh
#!/bin/bash

while getopts "a:b:cdef" opt; do
case $opt in
a)
echo "this is -a the arg is ! $OPTARG"
;;
b)
echo "this is -b the arg is ! $OPTARG"
;;
c)
echo "this is -c the arg is ! $OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG"
;;
esac
done
[hello@Git shell]$


执行结果结合代码显而易见。同样你也会看到有些代码在a的前面也会有冒号,比如下面的

情况一,没有冒号:
[cpp] view plain copy
[hello@Git shell]$ bash test.sh -a hello
this is -a the arg is ! hello
[hello@Git shell]$ bash test.sh -a
test.sh: option requires an argument -- a
Invalid option: -
[hello@Git shell]$ more test.sh
#!/bin/bash

while getopts "a:" opt; do
case $opt in
a)
echo "this is -a the arg is ! $OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG"
;;
esac
done
[hello@Git shell]$


情况二,有冒号:
[hello@Git shell]$ bash test.sh -a hello
this is -a the arg is ! hello
[hello@Git shell]$ bash test.sh -a
[hello@Git shell]$ more test.sh
#!/bin/bash

while getopts ":a:" opt; do
case $opt in
a)
echo "this is -a the arg is ! $OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG"
;;
esac
done

情况一输入 -a 但是后面没有参数的的时候,会报错误,但是如果像情况二那样就不会报错误了,会被忽略。

getopts option_string variable
当optstring以”:”开头时,getopts会区分invalid option错误和miss option argument错误。invalid option时,varname会被设成?,$OPTARG是出问题的option;
miss option argument时,varname会被设成:,$OPTARG是出问题的option。
如果optstring不以”:“开头,invalid option错误和miss option argument错误都会使varname被设成?,$OPTARG是出问题的option。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  如何 option specified