【Linux 】getopts 可选参数_Bash技巧:介绍 getopts 内置命令解析选项参数的用法

1. 概述

在 Linux bash shell 中,内置了一个 getopts 命令,可以处理以 ‘-’ 开头的选项参数。本篇文章通过多个实例详解 getopts 命令的用法。

getopts 命令简介
在 bash shell 上执行命令,常常会用到一些选项参数来指定不同的操作。例如 ls 命令的 -l、-a 选项等。

我们在编写 shell 脚本时,也可以自定义一些选项参数,并使用 bash 的 getopts 内置命令来解析选项参数。

查看 man bash 里面对 getopts 内置命令的英文说明如下:

getopts optstring name [args]
getopts is used by shell procedures to parse positional parameters.

optstring contains the option characters to be recognized; if a character is followed by a colon, the option is expected to have an argument, which should be separated from it by white space.


Each time it is invoked, getopts places the next option in the shell variable name, initializing name if it does not exist, and the index of the next argument to be processed into the variable OPTIND.

When the end of options is encountered, getopts exits with a return value greater than zero.
OPTIND is set to the index of the first non-option argument, and name is set to ?.

注意:getopts 是 bash 的内置命令。对于 bash 内置命令来说,不能用 man 命令查看它们的帮助说明
要使用 help 命令查看。也可以在 man bash 里面搜索命令名称查看相应的说明。

$ man getopts

No manual entry for getopts

$ help getopts

getopts: getopts optstring name [arg]

Parse option arguments.

可以看到,man getopts 提示找不到 getopts 命令的说明,而 help getopts 打印了它的说明。

另外,有一个 getopt 外部命令也可以解析命令选项,名称比 getopts 少了一个 s,用法也有所差异,不要把这两个命令搞混了

2. 命令详解

getopts optstring name [args]

基于 getopts optstring name [args] 这个命令格式,对 getopts 命令各个参数的含义说明如下。

optstring 参数:

  • 指定支持的选项参数列表,每个字符对应一个选项。
  • 如果字符后面跟着冒号 :,那么在输入该选项时预期后面跟着一个参数,选项和参数之间用空格隔开。
  • 不能使用冒号 : 和问号 ? 来作为选项,即:和?是特殊关键字。
选项内容 说明
       : optsring如果以:开头,表示是静默模式,忽略一般错误消息
      s 有效选项并且后面不带参数值
      s: 有效选项并且后面必须带参数值

完整用法示例 code1,保存为./1-4.sh:

#!/bin/bash
 
#input paramters index
echo "OPTIND starts at $OPTIND"
#get paramters
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 "OPTION is now $OPTIND"
done

例如,一个有效的 optstring 参数值是 “hi:”。
那么 -h 就是一个选项;-i 也是一个选项。
由于在 i 后面跟着冒号 :,那么输入 -i 选项时还要提供一个参数,如 -i insert 等。
如果实际执行的时候,在 -i 后面不提供参数,getopts 命令会报错。

注意:optstring 参数的选项列表不包含 - 字符,但是在实际输入选项参数时,getopts 命令要求选项参数以 - 开头,否则会报错。

以上面例子来说,-h 是一个选项,但是 h 并不是一个有效的选项( h虽不是有效选项,但也不会报错)。

2.1 name

name 参数用于保存解析后的选项名。每调用一次 getopts 命令,它只解析一个选项,并把解析的值存入 name 变量中
解析后的值不包含 - 字符。

例如解析 -h 选项后,name 变量的值是字符 h。
该变量的名称不要求只能是 name 字符串,也可以是其他合法的变量名,例如 opt、arg 等等。

如果要解析多个选项时,需要在 while 或者 for 循环中多次执行 getopts 命令,来逐个解析参数选项,直到解析完毕为止。
解析完毕,getopts 命令会返回 false,从而退出循环

如果提供的选项不在 optstring 指定的列表里面,name 的值会被设成问号 ?
但是 getopts 命令还是返回true,不会报错。

参见上面完整示例,这就是Case中有?号的原因

2.2 args

[args] 是一个可选参数,用于指定选项参数的来源。
getopts 命令默认解析位置参数提供的参数,例如 $1、$2、…、等等。

如果提供了 args 参数,那么从 args 中解析选项参数,不再从位置参数中解析。即for循环的内容会来自指定项,而不是全部的入参

  • 也就是说,在 shell 脚本里面直接执行 getopts 命令,它默认解析的选项参数是执行脚本时提供的参数。
    例如有一个 testgetopts.sh 脚本,那么执行 ./testgetopts.sh -a -b 命令,getopts 会解析 -a、-b 选项。

  • 如果是在函数内执行 getopts 命令,它解析的选项参数是调用函数时提供的参数。
    例如有一个 test_getopts 函数,该函数内调用 getopts 命令,那么执行 test_getopts -a -b 语句,getopts 命令会解析 -a、-b 选项。

  • 如果提供了 args 参数,getopts 命令改成解析 args 参数包含的选项。
    例如执行 args="-a -b"; getopts "ab" opt $args 语句,getopts 命令解析的是 args 变量指定的 “-a -b” 字符串,如果执行 ./testgetopts.sh -a -b -c,那么-c就会被忽略掉。

2.3 OPTARG

OPTARG 是 getopts 命令用到的一个全局变量,保存解析出来的带冒号选项后面的参数值。
例如解析上面提到的 -i insert 选项,那么 OPTARG 的值就是 insert。

2.4 OPTIND

OPTIND 是 getopts 命令用到的一个全局变量,保存下一个待解析的参数index。
当启动新的shell时,OPTIND 的默认值是 1,调用一次 getopts 命令,OPTIND 的值加 1。
如果带冒号的选项后面提供了参数,OPTIND 的值会加 2。
当 getopts 命令解析完所有参数后,shell 不会自动重置 OPTIND 为 1。

如果在同一个 shell 脚本里面要解析不同的选项参数,需要手动为 OPTIND 赋值为 1,否则会解析到不预期的选项。
后面会以一个 testgetopts.sh 脚本为例进行说明。

2.5 getopts 命令的返回值

查看 man bash 里面对 getopts 命令的返回值说明如下:

getopts returns true if an option, specified or unspecified, is found.
It returns false if the end of options is encountered or an error occurs.

可以看到,即使提供不支持的选项,getopts 命令也是返回true

当解析完所有选项后,getopts 会返回 false,遇到错误时也会返回 false。
遇到错误的情况有如下几种:

  • 选项没有以 - 开头
  • 带有冒号的选项要求后面提供一个参数,但是没有提供该参数

这个返回值有什么用?我们参见上面的code1 ,一般用在while循环中:


while getopts ":pq:" optname    //这里说明只接受 pg作为有效参数,且p不要求value,q必须带value

当循环过程中,如果出现符合false条件,就会终止循环,我们来验证下:

这是完全符合条件的入参:

$./1-4.sh  -p -q 12    //这是完全符合条件的入参
OPTIND starts at 1
Option p is specified    //p参数被迭代出来
OPTION is now 2
Option q has value 12      q参数被迭代出来
OPTION is now 4

我们故意加入一个不存在的入参 -m试试:

./1-4.sh  -p -m -q 12                  //在p q之间我们加入 -m,注意m前有-
OPTIND starts at 1
Option p is specified
OPTION is now 2
./1-4.sh: illegal option -- m        //m入参不在允许的范围内,但仍返回true,得以让while循环体继续,被转为 ?,进入case的?号分支
Unknown option
OPTION is now 3
Option q has value 12         //q仍然被打印,说明-m返回的是true,得以让while循环体继续
OPTION is now 5

这次,我们加入m试试,注意,不带-:

 ./1-4.sh  -p m -q 12
OPTIND starts at 1
Option p is specified
OPTION is now 2      
          //这里么有q,说明  m 被赋返回值 false,导致循环终止了

3. testgetopts.sh 脚本示例

下面以一个 testgetopts.sh 脚本为例来说明 getopts 命令的用法,其内容如下:

#!/bin/bash
    
function test_getopts_ab()
{
    
    
    local opt_ab
    while getopts "ab" opt_ab; do        #函数内调用
        echo $FUNCNAME: $OPTIND: $opt_ab
    done
}
    
echo Before Call test_getopts_ab: OPTIND: $OPTIND ++
test_getopts_ab "-a" "-b"
echo After Call test_getopts_ab: OPTIND: $OPTIND --     #函数外  打印OPTIND,是3,说明是累加的,全局变量
    
while getopts "ef" opt_ef; do     #脚本直接调用
    echo $OPTIND: $opt_ef
done
    
OPTIND=1
echo Reset OPTIND to: $OPTIND
    
number=6;
while getopts "s:g" opt_sg; do
    case $opt_sg in
        g) echo $number ;;
        s) number=$OPTARG ;;
        ?) echo "unknown option: $opt_sg" ;;
    esac
done

这个脚本先在 test_getopts_ab 函数中调用 getopts 命令,解析传入的 “-a” “-b” 选项。
然后调用 getopts 命令解析执行脚本时传入的命令行选项参数。
最后重置 OPTIND 的值,重新解析命令行选项参数。

以 getopts “ab” opt_ab 语句来说,“ab” 对应上面提到的 optstring 参数,它支持的选项就是 -a、-b。
opt_ab 对应上面提到的 name 变量名,保存解析得到的选项,不包含 - 字符。

执行这个脚本,结果如下:

$ ./testgetopts.sh -s 7 -g -f

Before Call test_getopts_ab: OPTIND: 1 ++

test_getopts_ab: 2: a

test_getopts_ab: 3: b

After Call test_getopts_ab: OPTIND: 3 --

./testgetopts.sh: illegal option -- g

4: ?

5: f

Set OPTIND to: 1

7

./testgetopts.sh: illegal option -- f

unknown option: ?

  • 可以看到,test_getopts_ab 函数解析完选项参数后,在函数外打印 OPTIND 的值是 3。

  • 之后再次调用 getopts 命令,OPTIND 值没有从 1 开始,还是从 3 开始取值,取到了传给 testgetopts.sh 的第三个参数 -g,跳过了前面的 -s 7 两个参数。

    这并不是预期的结果。正常来说,预期是从第一个选项参数开始解析。
    由于 getopts “ef” opt_ef 语句不支持 -g 选项,打印报错信息,并把 opt_ef 赋值为问号 ?。

  • 手动将 OPTIND 值重置为 1 后,getopts “s:g” opt_sg 可以从第一个选项参数开始解析。
    先处理 -s 7 选项,getopts 把 7 赋值给 OPTARG,脚本里面再把 OPTARG 的值赋给 number 变量。

    然后继续处理 -g 选项,打印出 number 变量的值。

    最后处理 -f 选项,该选项不支持,opt_sg 的值被设成问号 ?,打印 “unknown option” 的信息。

    处理完所有选项后,getopts 返回 false,退出 while 循环。

OPTIND 是全局参数

4. 错误判断

getopts 命令处理完选项参数、或者遇到错误时,都会返回 false,不能通过判断返回值来确认是否遇到了错误

当在 while 或者 for 循环中调用 getopts 时,可以通过 OPTIND 的值来判断 getopts 是否遇到了错误。如果 OPTIND 的值减去 1 后,不等于传入的参数个数,那么就是遇到了错误导致提前退出循环。

当 getopts 处理选项参数时,OPTIND 的值从 1 开始递增。
处理所有参数后,OPTIND 指向最后一个参数,相当于是所有参数个数加 1。
所以 OPTIND 的值减去 1 就应该等于传入的参数个数。

Bash 的 $# 表达式可以获取传入的参数个数,如果这两个值不相等,那么 getopts 就没有解析完选项参数,也就是遇到了错误导致提前退出循环。

假设有一个 test.sh 脚本,内容如下:

#!/bin/bash
while getopts "abcd" arg; do
    echo $OPTIND: $arg
done
    
echo OPTIND: $OPTIND. $#: $#
if [ "$(($OPTIND - 1))" != "$#" ]; then
    echo Error occurs.
fi

传入不同的选项参数,执行该脚本的结果如下:

$ ./test.sh -a -b -c
2: a
3: b
4: c
OPTIND: 4. $#: 3

$ ./test.sh -a -b
2: a
3: b
OPTIND: 3. $#: 2

$ ./test.sh -a b
2: a
OPTIND: 2. $#: 2
Error occurs.

可以看到,当正常遇到选项末尾时,OPTIND 变量的值是选项个数加 1。
当遇到错误时,OPTIND 变量的值不是选项个数加 1。
所以当 OPTIND 变量的值减去1,不等于 $# 时,就表示遇到了错误。

5. 通过 source 多次执行脚本对 OPTIND 的影响

通过 source 命令调用脚本是运行在当前 shell 下,由于 shell 不会自动重置 OPTIND 的值,如果要调用的脚本使用了 getopts 命令解析选项参数,在每次调用 getopts 之前,一定要手动重置 OPTIND 为 1,否则 OPTIND 的值不是从 1 开始递增,会获取到不预期的选项参数值。

假设有一个 test.sh 脚本,其内容如下:

#!/bin/bash

echo $#: $#, $@: $@, OPTIND: $OPTIND

getopts "abc" opt

echo $?, $opt

分别不使用 source 命令和使用 source 命令执行该脚本,结果如下:

$ ./test.sh -a

$#: 1, $@: -a, OPTIND: 1

0, a

$ ./test.sh -b

$#: 1, $@: -b, OPTIND: 1

0, b

$ source ./test.sh -a

$#: 1, $@: -a, OPTIND: 1

0, a

$ source ./test.sh -b

$#: 1, $@: -b, OPTIND: 2

1, ?

可以看到,执行 ./test.sh -a 命令和 ./test.sh -b 命令的输出结果都正常。
执行 source ./test.sh -a 命令的结果也是正常。
但是接着执行 source ./test.sh -b 命令,调用 getopts 之前,打印出 OPTIND 的值是 2,要获取第二个选项参数。
由于没有提供第二个选项参数,获取到的选项参数值是问号 ?,用 $? 获取 getopts 命令的返回值是 1,执行报错。

即,如果一个脚本使用了 getopts 命令,而该脚本又要用 source 命令来执行时,脚本需要手动设置 OPTIND 变量的值为1,否则会遇到上面的异常。

当我们自己写了一个脚本,并在 .bashrc 文件中配置 alias 使用 source 命令来执行该脚本,例如下面的设置:

alias c=‘source quickcd.sh’

当 quickcd.sh 脚本使用了 getopts 命令,且没有重置 OPTIND 的值,那么多次执行 c 这个命令,就会遇到上面描述的异常。

参考

getopts 可选参数_Bash技巧:介绍 getopts 内置命令解析选项参数的用法

猜你喜欢

转载自blog.csdn.net/m0_45406092/article/details/133358134
今日推荐