2012-11-24 119 views
0

我可以發誓這是 - 只是 - 工作正常。現在我不知道爲什麼我會收到我收到的錯誤。關聯數組鍵中的未綁定變量

這裏是我的腳本:

115: echo "file is $file" 
116: echo "key is $key" 
117: if [[ ! ${stats["$file-$key"]:-} ]]; then 
118:  stats["$file-$key"]=0 
119:  echo "set stats[$file-$key] to 0" 
120:  echo "$file-$key" 
121:  echo "stats is ${stats["$file-$key"]}" 
122: fi 
123: stats["$file-$key"]=$((value + stats["$file-$key"])) 

這裏是我的輸出:

file is test1 
    key is key1 
    set stats[test1-key1] to 0 
    test1-key1 
    stats is 0 
    file is test1 
    key is key2 
    set stats[test1-key2] to 0 
    test1-key2 
    stats is 0 
    file is test1 
    key is key3 
    set stats[test1-key3] to 0 
    test1-key3 
    stats is 0 
    file is test1 
    key is key4 
    set stats[test1-key4] to 0 
    test1-key4 
    stats is 0 
    file is test1 
    key is key1 
    ./runMe.sh: line 117: test1: unbound variable 

這似乎只是正常工作的第4次迭代。然後當它試圖測試第一個密鑰是否被設置(它在這一點上)時失敗。到底是怎麼回事?

+0

請顯示全部代碼。 –

回答

0

我被兩個運行之間調用此:

unset stats 

我認爲它不會undeclare什麼變量是。