在bash中的case语句中使用命令args

时间:2016-05-18 23:03:45

标签: 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

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

1 个答案:

答案 0 :(得分:2)

不要试图解决你的位置参数,而是将它们排除在外。这使您的逻辑更加简单,并且您的命令行界面更加用户友好(因为在这种情况下它可以具有位置参数选项,这实际上是命令行的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; }