【问题标题】:getopts: optional $OPT_ARGgetopts:可选 $OPT_ARG
【发布时间】:2012-10-14 20:28:39
【问题描述】:

我希望创建一个可以同时运行 -u 和 -u gtest_filter=

的脚本

目前我有

当 getopts u: opt 2> /dev/null;做

案例$选择加入

u) UNIT_TEST=1; UNIT_TEST_OPTION=$OPTARG ;;

?) 用法 >&2;出口 1 ;;

esac

完成

但是必须有一个参数。我该如何解决这个问题,以便 -u 也可以工作。

【问题讨论】:

    标签: getopts


    【解决方案1】:

    使用u 代替u:(去掉冒号)。然后检查下一个参数是否以-开头

    while getopts u opt 2> /dev/null; do
    case $opt in
    u)
        UNIT_TEST=1;
        # Test first character of next option
        if [ "$(echo "$2" | head -c 1)" != "-" ]; then
            UNIT_TEST_OPTION=$2
            shift # Move on to next option
        fi
        ;;
    ?) usage >&2; exit 1 ;;
    esac
    done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      • 2018-01-25
      • 2019-05-09
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多