【问题标题】:Is optstring in getopt case sensitive?getopt 中的 optstring 是否区分大小写?
【发布时间】:2019-04-08 06:09:25
【问题描述】:

下面的v 是否也会解析-V 选项?

getopt -o v

甚至可以解析大写命令选项吗?

【问题讨论】:

    标签: getopt


    【解决方案1】:

    回答您的问题 - getopt 区分大小写,通常不建议在脚本参数中使用不同的大小写 - 这会造成混淆

    您可以考虑在其中使用多字符输入。

    尝试阅读有关 getopt --longoptions 的信息。

    请参考以下示例。

    # Read command line options
    ARGUMENT_LIST=(
        "input1"
        "input2"
        "input3"
    )
    
    
    
    # read arguments
    opts=$(getopt \
        --longoptions "$(printf "%s:," "${ARGUMENT_LIST[@]}")" \
        --name "$(basename "$0")" \
        --options "" \
        -- "$@"
    )
    
    
    echo $opts
    
    eval set --$opts
    
    while true; do
        case "$1" in
        --input1)  
            shift
            empId=$1
            ;;
        --input2)  
            shift
            fromDate=$1
            ;;
        --input3)  
            shift
            toDate=$1
            ;;
          --)
            shift
            break
            ;;
        esac
        shift
    done
    

    这就是你可以调用脚本的方式

    myscript.sh --input1 "ABC" --input2 "PQR" --input2 "XYZ"
    

    试试这个,希望有用

    【讨论】:

      猜你喜欢
      • 2011-11-16
      • 1970-01-01
      • 2012-11-07
      • 2013-03-16
      • 2012-08-07
      • 2012-03-11
      • 2013-10-25
      • 2011-09-02
      相关资源
      最近更新 更多