"Can't shift that many" dash case 语句错误

"Can't shift that many" error in dash case statement

我是 运行 Arch Linux 并且在 Dash 中编写脚本。

我有一个虚拟脚本,它输出我使用参数设置的所有变量。我遵循了一个建议以这种方式获取输入的教程。 Link to article

while [ ! -z "" ]; do
    case "" in
        --param|-p)
            shift
            #code
            ;;
        --other-param|-op)
            shift
            # code
            ;;
    esac
shift
done

在下面的 MRE 中,输入 -e 参数会在该参数代码内的 shift 行中产生错误“shift: can't shift that many”。它是一致的,我不知道它是什么意思。

为什么会抛出这个错误?

我的 MRE 脚本是 运行 破折号 shell,如下所示。

#!/bin/dash

while [ ! -z "" ]; do
    case "" in
        -e)
            shift
            echo "Error right about..."
            ;;
    esac
shift
done

./mre.sh -e

的输出
Error right about...
./mre.sh: 32: shift: can't shift that many

问题是 esac 之后的 shift。您已经在演示中处理了 "$@" 中的所有参数,因此没有什么可以移动的了。

您正在学习的教程试图演示选项 带有参数 -t 12 如何在第一次转换后将参数留在 </code> 中,并且然后在完成后将其关闭。 (我不会这样做;第二次转变应该在 <code>case 内发生,当您完成该特定选项时,这样您就不会强制 all 您的选项需要一个参数。)

Bash 在这方面与 Dash 完全一样,尽管当它不能再 shift 时它不会给你明确的错误消息。