使用getopts命令

使用getopts命令

Content #

getopts(注意是复数)是bash shell的内建命令,和近亲getopt看起来很像,但多了一些扩展功能。

getopt与getopts的不同之处在于,前者在将命令行中选项和参数处理后只生成一个输出,而后者能够和已有的shell位置变量配合默契。

getopts每次只处理一个检测到的命令行参数。在处理完所有的参数后,getopts 会退出并返回一个大于0的退出状态码。这使其非常适合用在解析命令行参数的循环中。

getopts命令的格式如下:

getopts optstring variable

有效的选项字母会在optstring中列出,如果选项字母要求有参数值,就在其后加一个冒号。不想显示错误消息的话,可以在optstring之前加一个冒号。 getopts命令会将当前参数保存在命令行中定义的variable中。

getopts命令要用到两个环境变量。如果选项需要加带参数值,那么OPTARG环境变量保存的就是这个值。OPTIND环境变量保存着参数列表中getopts正在处理的参数位置。这样在处理完当前选项之后就能继续处理其他命令行参数了。

下面来看一个使用getopts命令的简单例子:

$ cat extractwithgetopts.sh
#!/bin/bash
# Extract command-line options and values with getopts
#
echo
while getopts :ab:c opt
do
     case "$opt" in
          a) echo "Found the -a option" ;;
          b) echo "Found the -b option with parameter value $OPTARG";;
          c) echo "Found the -c option" ;;
          *) echo "Unknown option: $opt" ;;
     esac
done
exit
$
$ ./extractwithgetopts.sh -ab BValue -c

Found the -a option
Found the -b option with parameter value BValue
Found the -c option
$

while语句定义了getopts命令,指定要查找哪些命令行选项,以及每次迭代时存储它们的变量名(opt)。

你会注意到在本例中case语句的用法有些不同。在解析命令行选项时,getopts 命令会移除起始的连字符,所以在case语句中不用连字符。

getopts命令有几个不错的特性。对新手来说,可以在参数值中加入空格: $ ./extractwithgetopts.sh -b “BValue1 BValue2” -a

Found the -b option with parameter value BValue1 BValue2 Found the -a option $ 另一个好用的特性是可以将选项字母和参数值写在一起,两者之间不加空格: $ ./extractwithgetopts.sh -abBValue

Found the -a option Found the -b option with parameter value BValue $ getopts命令能够从-b选项中正确解析出BValue值。除此之外,getopts命令还可以将在命令行中找到的所有未定义的选项统一输出成问号:

$ ./extractwithgetopts.sh -d

Unknown option: ? $ $ ./extractwithgetopts.sh -ade

Found the -a option Unknown option: ? Unknown option: ? $ optstring中未定义的选项字母会以问号形式传给脚本。

getopts命令知道何时停止处理选项,并将参数留给你处理。在处理每个选项时, getopts会将OPTIND环境变量值增1。处理完选项后,可以使用shift命令和 OPTIND值来移动参数:

$ cat extractoptsparamswithgetopts.sh
#!/bin/bash
# Extract command-line options and parameters with getopts
#
echo
while getopts :ab:cd opt
do
     case "$opt" in
          a) echo "Found the -a option" ;;
          b) echo "Found the -b option with parameter value $OPTARG";;
          c) echo "Found the -c option" ;;
          d) echo "Found the -d option" ;;
          *) echo "Unknown option: $opt" ;;
     esac
done
#
shift $[ $OPTIND - 1 ]
#
echo
count=1
for param in "$@"
do
     echo "Parameter $count: $param"
     count=$[ $count + 1 ]
done
exit
$
$ ./extractoptsparamswithgetopts.sh -db BValue test1 test2

Found the -d option
Found the -b option with parameter value BValue

Parameter 1: test1
Parameter 2: test2
$

From #

Linux命令行与shell脚本编程大全