如何处理 Zsh 中的 Error "shift count must be <= $#"?

How to handle Error "shift count must be <= $#" in Zsh?

我想构建这样的命令参数:

$ command <args> <parameter>

$ cut -d ' '

构建命令的代码是这样写的:

function command(){
  if [ "$#" -gt 0 ]; then
    while [ "$#" -gt 0 ]; do
      case "" in
        '-t'|'--type')
          type_parameter=
          shift 2
        ;;
        * )
          shift 1
        ;;
      esac
    done
  fi
}

而且我想检测如果特殊<args>之后没有任何参数存在,那么我可以编写一个错误处理程序代码。

$ command -t 123 # this is the input value I want.
good input!!

$ command -t     # I want to detect this situation happen.
wrong input!!

但是现在我遇到了错误:

$ command -t 
command:shift:101: shift count must be <= $#

不知道怎么解决,请问有什么办法可以完美处理或者避免吗?

简单的通过测试参数个数。除了无条件地执行 shift 2,您还可以执行类似

的操作
 if (( $# >= 2 ))
 then
   shift 2
 else
   echo "You need to provide an argument to -z!"
   exit 21
 fi

这将允许您打印自己的错误消息。当然,如果你不想将缺少的选项参数视为错误,你也可以从循环中跳出(因为你知道无论如何都不会留下任何参数)。