在 bash 的 case 语句中使用命令参数

the*_*ish 1 linux bash case

我有一个 case 语句,它是我命令的一部分,我希望它遍历所有 args,直到它们都被消耗掉。

while [ ! -z ${@:2} ] ; do
    case "${@:2}" in
        -d|--delete*)
            YES_DELETE=1
            shift
            ;;        
        -nd|--nodelete*)
            NO_DELETE=1
            shift
            ;;
        -y|--yes*)
            SKIP_PROMPT=1
            shift
            ;;
        -*|*)
            echo "Bad command, try again."
            ;;
    esac
done
Run Code Online (Sandbox Code Playgroud)

我的命令$@$ mpip stt=12 -nd -y使${@:2}="-nd -y". 如果它也像我想要的那样工作,它会在迭代两次后退出 while 循环并且NO_DELETE=1 SKIP_PROMPT=1为真。当我运行它时,一切都还没有初始化,它变成了一个无限循环,我不知道我做错了什么。

Cha*_*ffy 5

不要试图解决您的位置参数,而是将它们弹出。这使您的逻辑更加简单,并且您的命令行界面更加用户友好(因为在这种情况下它可以在选项后面有位置参数,这实际上是用于命令行解析的 POSIX 标准公式——实际上,POSIX 工具根本不需要允许选项跟随位置参数,尽管 GNU 工具通常作为扩展这样做)。

#!/bin/bash
#      ^^^^- IMPORTANT: Not /bin/sh

args=( ) # positional arguments

# completely empty the argv, shifting positional options into "${args[@]}" and processing
# option flags.
while (( $# )); do
  case $1 in
    -d)   yes_delete=1 ;;
    -nd)  no_delete=1 ;;
    -y)   skip_prompt=1 ;;
    -*)   echo "Bad option" >&2; exit 1 ;;
    --)   shift; args+=( "$@" ); set -- ;;
    *)    args+=( "$1" ) ;;
  esac
  shift
done

# with options popped off, make our positional arguments the whole argument list
set -- "${args[@]}"

# examples of positional argument validation
(( "$#" )) || { echo "Error: At least one argument is required" >&2; exit 1; }
[[ $1 != *=* ]] || { echo "Error: First argument must contain an = sign" >&2; exit 1; }
Run Code Online (Sandbox Code Playgroud)