使用 set -u 进行 bash 调试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5408405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash debugging using set -u
提问by hornetbzz
I have a bash sourcedbash.shsourced in another bash main code main.sh.
Running main.shwith the "set -u" option, I'm getting an error than I can't figure out :
我有一个 来自另一个 bash 主代码main.sh的 bash sourcedbash.sh。使用“ set -u”选项
运行main.sh,我收到一个我无法弄清楚的错误:
Error /sourced_bash.sh : line xx : variable without link
main.sh
主文件
. sourced_bash.sh
my_function $foomain 1
sourcedbash.sh
源bash.sh
function my_function(){
local foo=
local bar=
if [[ 1 -eq $bar ]];then # <= this is LINE xx generating the error
# ... dothis
return 1
elif [[ 0 -eq $bar ]];then
# ... dothat
return 0
fi
}
Looked into the man pages and reading "my friend" w/o a frank success.
查看手册页并阅读“我的朋友”,并取得了坦率的成功。
I'd need to understand why "set -u" implies the main.shprogram aborption and how to get rid of this error (Debian Lenny).
我需要了解为什么“ set -u”意味着main.sh程序中止以及如何摆脱这个错误(Debian Lenny)。
Thx in advance
提前谢谢
回答by dogbane
Since you are doing string comparison, you need to use quotes and ==. Try changing to:
由于您是在进行字符串比较,因此需要使用引号和 ==。尝试更改为:
if [[ "1" == "$bar" ]] || [[ "true" == "$bar" ]];then
Update:
更新:
What is $foomain? Has it been set?
什么是$foomain?已经设置了吗?
set -umakes Bash check whether you have initialised all your variables. If you haven't, Bash will throw an error about unbound variables.
set -u让 Bash 检查您是否已初始化所有变量。如果没有,Bash 会抛出一个关于未绑定变量的错误。

