gpt4 book ai didi

linux - 使用 set -u 进行 bash 调试

转载 作者:太空宇宙 更新时间:2023-11-04 09:55:27 24 4
gpt4 key购买 nike

我有一个 bash sourcedbash.sh 源自另一个 bash 主代码 main.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

function my_function(){
local foo=$1
local bar=$2
if [[ 1 -eq $bar ]];then # <= this is LINE xx generating the error
# ... dothis
return 1
elif [[ 0 -eq $bar ]];then
# ... dothat
return 0
fi
}

查看手册页并阅读“我的 friend ”,但没有取得坦率的成功。

我需要理解为什么“set -u”意味着 main.sh 程序中止以及如何摆脱它错误(Debian Lenny)。

提前致谢

最佳答案

由于是做字符串比较,所以需要用到引号和==。尝试更改为:

   if [[ "1" == "$bar" ]] || [[ "true" == "$bar" ]];then

更新:

什么是$foomain?设置了吗?

set -u 使 Bash 检查您是否已初始化所有变量。如果没有,Bash 将抛出有关未绑定(bind)变量的错误。

关于linux - 使用 set -u 进行 bash 调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5408405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com