我有一个循环来解析命令行参数。如果 -d 或 -b 参数有参数,一切正常。但是当我在没有参数的情况下传递 -d 或 -b 时,脚本会进入无限循环。
# Parse command line input
while [ "$#" -gt 0 ]; do
case "$1" in
-d) DOMAIN="$2"; shift 2;;
-b) BITS="$2"; shift 2;;
*) die "unrecognized argument: $1"
esac
done
如何在空参数上抛出错误?
由于参数不足,Shift 失败。
但是,这个问题会立即显而易见,因为您将 shell 设置为在所有错误上失败(使用 set -e
)。 在许多情况下,它通过避免静默故障使调试变得更加容易,并强制执行纪律和错误检查。 set -u
会导致失败的变量扩展错误,也非常有用。
如果尝试移动的值数超过列表中存在的值数,则 shift n
命令不会执行任何操作。 这就是代码中无限循环的原因。
使用getopts
是正确的做法,因为它为您提供了很大的灵活性。 否则,您可以通过以下方式重写循环:
#!/bin/bash
# Parse command line input
d_flag=0
b_flag=0
while [ "$#" -gt 0 ]; do
case "$1" in
-d)
d_flag=1
shift
[[ $1 == -* ]] && continue # argument to -d not given
DOMAIN=$1
shift
;;
-b)
b_flag=1
shift
[[ $1 == -* ]] && continue # argument to -b not given
BITS=$1
shift
;;
*) echo "unrecognized argument: $1"; exit 2;;
esac
done
if [[ $d_flag = 1 && -z $DOMAIN ]]; then
# handle error - option requires an argument
fi
if [[ $b_flag = 1 && -z $BITS ]]; then
# handle error - option requires an argument
fi
由于您似乎总是需要两个参数,因此您可以将第一行更改为:
while [ "$#" -eq 2 ]; do
否则,您可以在进入循环之前检查 $2 的值,如下所示:
if [[ -z $2 ]]; then
echo "missing second parameter"
else
#do the while loop
fi