【问题标题】:Is there a way to have a string as a switch using getopts?有没有办法使用 getopts 将字符串作为开关?
【发布时间】:2010-08-05 15:56:42
【问题描述】:

我正在查看getopts 是否有办法使用字符串而不是字符来处理开关。

例如,我想提供如下内容:

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

代替:

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

unix getopts 可以做到这一点吗?

【问题讨论】:

    标签: ksh unix getopts


    【解决方案1】:

    不,getopts 不可能。您必须自己进行解析,例如带有case 开关:

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

    【讨论】:

    • 我感觉就是这样。非常感谢!
    【解决方案2】:

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

    来自BashFAQ/035

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-28
      • 1970-01-01
      • 2023-01-30
      • 2020-07-11
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      • 2012-03-16
      相关资源
      最近更新 更多