getops :限制輸入選項的條件

getops :限制輸入選項的條件

我有一個 shell 腳本,它接受 4 個輸入選項 (x:m:b:v)。但我想限制我的輸入選項。

條件1

1. Accept option x or x and b
   eg: sh script -x <arg> (or)             ;will execute function a defined in script
       sh script -x <arg> -b               ;will execute the functions a and b

條件2

2. Accept option m or m and b
   eg: sh script -m <arg> (or)     ;execute function c
       sh script -m <arg1> -b   ;should execute function for c and b

條件3

3. Option both x and m should not be passed together.
   This will print usage(how to use the input options) function.

條件4

4. Option v. This should not be passed with any other option.
   eg: sh script -v <arg>  ; executs the function for v

我知道這可以透過 if 和 else 條件來實現,但我無法獲得條件的邏輯。

此外,如果沒有傳遞任何輸入參數(或)如果傳遞了所有輸入參數,則應執行使用函數。

答案1

作為說明我上面的評論的一個例子,這裡是 FreeBSD 的摘錄/usr/src/bin/cp/cp.c

while ((ch = getopt(argc, argv, "HLPRafilnprsvx")) != -1)
        switch (ch) {
        case 'H':
                Hflag = 1;
                Lflag = 0;
                break;
        case 'L':
                Lflag = 1;
                Hflag = 0;
                break;
        ...
        case 'l':
                lflag = 1;
                break;
        ...
        case 's':
                sflag = 1;
                break;
        case 'v':
                vflag = 1;
                break;
        case 'x':
                fts_options |= FTS_XDEV;
                break;
        default:
                usage();
                break;
        }

請注意,上面設定了許多布林變量,如 Hflag、Lflag、lflag、sflag 等。另請注意,當使用者指定未定義的選項標誌時,default該語句的子句switch會強制呼叫。usage()

至於標誌排除,例如不允許mflag&& xflag,類似的情況出現在 cp.c 中:

if (lflag && sflag)
        errx(1, "the -l and -s options may not be specified together");

以下是該程式碼從 C 語言的可能翻譯,bash並對您的範例進行了額外的調整:

flagb=
flagm=
flagv=
flagx=

while getopts "bm:vx:" ch
do

        case "$ch" in
        [bv])
                eval flag$ch=1
                printf 'flag%s set\n' "$ch"
                ;;
        [mx])
                eval flag$ch=1
                eval arg$ch=\"$OPTARG\"
                printf 'flag%s set\n' "$ch"
                printf 'arg%s is "%s"\n' "$ch" "$OPTARG"
                ;;
        -)
                break
                ;;
        esac

done

[[ $OPTIND > 1 ]] && shift $(($OPTIND-1))

if [[ "$flagv" ]]
then
        if [[ -n "$flagb$flagm$flagx" ]]
        then
                printf 'error: -v cannot be used with any other flags\n' >&2
                exit 2
        fi
fi

if [[ "$flagm$flagx" = "11" ]]
then
        printf 'error: -m and -x cannot be used together\n' >&2
        exit 2
fi

if [[ "$flagb" ]] && ! [[ "$flagm$flagx" ]]
then
        printf 'error: -b requires either -m or -x\n' >&2
        exit 2
fi

[[ "$flagb" ]] && printf 'b flag is set\n'
[[ "$flagm" ]] && printf 'm flag is set, arg is "%s"\n' "$argm"
[[ "$flagv" ]] && printf 'v flag is set\n'
[[ "$flagx" ]] && printf 'x flag is set, arg is "%s"\n' "$argx"

[[ "$@" ]] && {
        printf 'remaining arguments:'
        printf ' "%s"' "$@"
        printf '\n'
}

相關內容