首页 > 解决方案 > 有没有办法限制 getopts 只能在单个选项上工作?

问题描述

我正在用 getopts 编写一个包含多个 case 语句的 bash 脚本。我的要求是能够决定什么时候 getopts 可以采取多种选择,什么时候不可以。

while getopts "hla:d:t:k:w:e:f:i:m:u:L:D:" options
do
    case $options in
        h)
            if [[ "${OPTARG}" = -* ]]; then
                echo "Multiple options are not allowed"
                exit 1
            fi
            verboseHelpAndExit
            break
            ;;
        l)
            if [[ "${OPTARG}" = -* ]]; then
                echo "Multiple options are not allowed"
                exit 1
            fi
            if [ "$#" -gt 0 ] || [ "$1" != '-l' ]; then
                echo "No parameters allowed"
            else
                list_users
            fi
            break
            ;;
        w)
            warn="$OPTARG"
            check_for_integer $warn
            ;;
        e)
            expire="$OPTARG"
            check_for_integer $expire
            ;;

这里 -h 和 -l 不应采用任何其他选项,如果通过任何其他选项,则应给出错误,但 -w 和 -e 可以采用多个选项,我的代码块有什么问题?

标签: bashgetopts

解决方案


推荐阅读