2013-07-31 39 views
1

在第二次插入關聯數組'originator'的代碼中,使第一次插入丟失。我檢查第一個插入是成功的,但是當我將第二個關聯項放入'originators'時,第一個項是空的,換句話說,它輸出併爲空字符串。我不知道會發生什麼事。第二次插入到關聯數組中的數據丟失

declare -A originators 

    while read -r line 
    do 
     if [ "$count" -ge "2" ]; 
     then 
      inner_count=0 
      #parse each line 
      if [ "$debug" = "1" ] ; then printf "%s\n" "$line" ; fi 

      for word in $line 
      do 

       if [ "$inner_count" = "1" ]; then tmp1="$word" ; fi 
       if [ "$inner_count" = "5" ]; then tmp1="$tmp1"" ---- ""$word" ;fi 
       inner_count=$((inner_count + 1)) 
      done  
       originators=(["$count"]="$tmp1") 
      echo "$count ${originators["$count"]}" 

     fi 
    count=$((count + 1)) 
    done < <(batctl tg) 

回答

2

你確實覆蓋該行的數組:

originators=(["$count"]="$tmp1") 

應改爲:

originators+=(["$count"]="$tmp1") 

+=運營商將新值追加到您的陣列。

+0

非常感謝您的解決方案。 – user1658296

+0

不客氣,很高興它解決了。 – anubhava