- ?*模式是否提供了 - *匹配未知命令行选项的任何优势?

时间:2014-09-20 08:43:51

标签: shell switch-statement

在“手动循环”部分的http://mywiki.wooledge.org/BashFAQ/035,他们有此代码。

#!/bin/sh
# (POSIX shell syntax)

# Reset all variables that might be set
file=
verbose=0

while :; do
    case $1 in
        -h|-\?|--help)   # Call a "show_help" function to display a synopsis, then exit.
            show_help
            exit
            ;;
        -f|--file)       # Takes an option argument, ensuring it has been specified.
            if [ "$2" ]; then
                file=$2
                shift 2
                continue
            else
                echo 'ERROR: Must specify a non-empty "--file FILE" argument.' >&2
                exit 1
            fi
            ;;
        --file=?*)
            file=${1#*=} # Delete everything up to "=" and assign the remainder.
            ;;
        --file=)         # Handle the case of an empty --file=
            echo 'ERROR: Must specify a non-empty "--file FILE" argument.' >&2
            exit 1
            ;;
        -v|--verbose)
            verbose=$((verbose + 1)) # Each -v argument adds 1 to verbosity.
            ;;
        --)              # End of all options.
            shift
            break
            ;;
        -?*)
            printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
            ;;
        *)               # Default case: If no more options then break out of the loop.
            break
    esac

    shift
done

# Suppose --file is a required option. Check that it has been set.
if [ ! "$file" ]; then
    echo 'ERROR: option "--file FILE" not given. See --help.' >&2
    exit 1
fi

# Rest of the program here.
# If there are input files (for example) that follow the options, they
# will remain in the "$@" positional parameters.

我试图理解从剧本中理解这些行。

    -?*)
        printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
        ;;

我一直在使用模式-*来匹配未知选项。但是这个脚本使用匹配未知的模式-?*

为什么-?*模式优先于模式-*提供匹配未知选项的优势?

1 个答案:

答案 0 :(得分:1)

单人短划线有时用于表示“从stdin读取”,
-?*不会理解。