2011-03-23 24 views
0

我有一個bash sourcedbash.sh在另一個bash的主代碼main.sh來源。
運行main.sh與 「集-u」 選項,我得到一個錯誤比我想不通:慶典調試使用set -u

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 
} 

看着手冊頁和閱讀「我的朋友」沒有坦率的成功。

我需要了解爲什麼「集-u」意味着main.sh程序吸收以及如何擺脫這種錯誤(Debian的萊尼)的。

Thx預先

+0

foomain實際上是5個字符串,根據「dothis」或「dothat」中的大小寫情況而有所不同,但錯誤指出「bar」上的測試行。 – hornetbzz 2011-03-23 16:52:38

回答

2

由於您在進行字符串比較,因此您需要使用引號和==。嘗試更改爲:

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

更新:

什麼是$foomain?它已被設置?

set -u使Bash檢查是否已初始化所有變量。如果你還沒有,Bash會拋出一個關於未綁定變量的錯誤。

+0

Thx - 這是一個轉錄錯誤,實際上我沒有使用字符串。 – hornetbzz 2011-03-23 16:42:48

+0

Thx,我編輯了這個問題,因爲使用字符串不是我猜測的那個點。 – hornetbzz 2011-03-23 18:38:07

+0

@hornetbzz你初始化了'$ foomain'嗎? – dogbane 2011-03-24 08:48:44