bash debugging using set -u
I have a bash sourcedbash.sh sourced in another bash main code main.sh开发者_如何学C.
Running main.sh with the "set -u" option, I'm getting an error than I can't figure out :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
}
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.sh program aborption and how to get rid of this error (Debian Lenny).
Thx in advance
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?
set -u
makes Bash check whether you have initialised all your variables. If you haven't, Bash will throw an error about unbound variables.
精彩评论