有没有办法让字符串作为使用getopts的开关?

时间:2010-08-05 15:56:43

标签: ksh unix getopts

我看到getopts是否有办法处理带字符串而不是字符的开关。

例如,我想提供这样的东西:

script.ksh -file1 file1.txt -file2.txt

而不是:

script.ksh -f file1.txt -g file2.txt

这是否可以使用unix getopts

2 个答案:

答案 0 :(得分:1)

外部getopt(注意没有“s”)可以处理长选项,但它有其自身的缺点。

来自BashFAQ/035

  

永远不要使用getopt(1)。 getopt无法处理空参数字符串或具有嵌入空格的参数。请忘记它曾经存在过。

答案 1 :(得分:0)

不,getopts无法实现。你必须自己解析,例如使用case开关:

while (($# > 0))
do
    case "$1" in
    -file1)
        shift
        file1=$1;;
    -file2)
        shift
        file2=$1;;
    esac
    shift
done
相关问题