【发布时间】:2017-06-17 02:13:27
【问题描述】:
我很抱歉自己没能找到这么看似微不足道的东西。
我需要向 shell 脚本 (Bash) 传递多个布尔参数,如下所示:
./script --parameter1 --parameter2
等等。
如果设置,所有都将被视为true。
在脚本的开头,我使用set -u。
带值传递的普通参数我目前的做法如下:
# this script accepts the following arguments:
# 1. mode
# 2. window
while [[ $# > 1 ]]
do
cmdline_argument="$1"
case $cmdline_argument in
-m|--mode)
mode="$2"
shift
;;
-w|--window)
window="$2"
shift
;;
esac
shift
done
我想添加类似的东西
-r|--repeat)
repeat=true
shift
;;
我不明白为什么它不能按预期工作。
它立即退出并出现错误:
./empire: line 450: repeat: unbound variable
第 450 行在哪里:
if [ "$repeat" == true ];
【问题讨论】:
-
你能解释一下它是如何失败的吗?是因为你错过了
shift? -
@GordonDavisson 我试过有没有
shift。 -
你在使用
set -u,不是吗? -
@CharlesDuffy 是的。
-
见BashFAQ #112。在相关说明中,一定在使用
set -e之前阅读 BashFAQ #105。
标签: bash shell parameter-passing