Bash - 附加到数组问题

时间:2017-06-05 14:35:04

标签: arrays bash

我对bash很新。

我有以下代码:

while read line
do
    foundproduct=`echo $line | awk -F "\"*,\"*" '{print $2}'`
    if [ "$foundproduct" == "$product" ]; then
        productid=`echo $line | awk -F "\"*,\"*" '{print $19}'`
        newid=1
        for i in ${productids[@]}
        do
            if [ "${productids[i]}" == "$productid" ] ; then
                newid=0
                break
            fi
        done
        if (( newid == 1 )) ; then
            declare -p productids
            echo "$productid"
            echo "1. $productids[${#productids[@]}]"
            productids+=("$productid")
            echo "2. $productids[${#productids[@]}]"
            declare -p productids
        fi          
        loadid=`echo $line | awk -F "\"*,\"*" '{print $39}'`
    fi
done < hddtable1.csv

产生以下结果:

declare -a productids='()'
ST900MM0006 
1. [0]
2. ST900MM0006 [1]
declare -a productids='([0]="ST900MM0006 ")'
declare -a productids='([0]="ST900MM0006 ")'
ST600MM0006 
1. ST900MM0006 [1]
2. ST900MM0006 [2]
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 ")'
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 ")'
ST600MM0006 
1. ST900MM0006 [2]
2. ST900MM0006 [3]
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 ")'
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 ")'
ST600MM0006 
1. ST900MM0006 [3]
2. ST900MM0006 [4]
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 ")'
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 ")'
ST600MM0006 
1. ST900MM0006 [4]
2. ST900MM0006 [5]
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 ")'
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 ")'
ST600MM0006 
1. ST900MM0006 [5]
2. ST900MM0006 [6]
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 ")'
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 ")'
ST600MM0006 
1. ST900MM0006 [6]
2. ST900MM0006 [7]
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 ")'
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 ")'
ST600MM0006 
1. ST900MM0006 [7]
2. ST900MM0006 [8]
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 " [7]="ST600MM0006 ")'
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 " [7]="ST600MM0006 ")'
ST300MM0006 
1. ST900MM0006 [8]
2. ST900MM0006 [9]
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 " [7]="ST600MM0006 " [8]="ST300MM0006 ")'
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 " [7]="ST600MM0006 " [8]="ST300MM0006 ")'
ST300MM0006 
1. ST900MM0006 [9]
2. ST900MM0006 [10]
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 " [7]="ST600MM0006 " [8]="ST300MM0006 " [9]="ST300MM0006 ")'
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 " [7]="ST600MM0006 " [8]="ST300MM0006 " [9]="ST300MM0006 ")'
ST300MM0006 
1. ST900MM0006 [10]
2. ST900MM0006 [11]
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 " [7]="ST600MM0006 " [8]="ST300MM0006 " [9]="ST300MM0006 " [10]="ST300MM0006 ")'
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 " [7]="ST600MM0006 " [8]="ST300MM0006 " [9]="ST300MM0006 " [10]="ST300MM0006 ")'
ST300MM0006 
1. ST900MM0006 [11]
2. ST900MM0006 [12]
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 " [7]="ST600MM0006 " [8]="ST300MM0006 " [9]="ST300MM0006 " [10]="ST300MM0006 " [11]="ST300MM0006 ")'
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 " [7]="ST600MM0006 " [8]="ST300MM0006 " [9]="ST300MM0006 " [10]="ST300MM0006 " [11]="ST300MM0006 ")'
ST300MM0006 
1. ST900MM0006 [12]
2. ST900MM0006 [13]
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 " [7]="ST600MM0006 " [8]="ST300MM0006 " [9]="ST300MM0006 " [10]="ST300MM0006 " [11]="ST300MM0006 " [12]="ST300MM0006 ")'
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 " [7]="ST600MM0006 " [8]="ST300MM0006 " [9]="ST300MM0006 " [10]="ST300MM0006 " [11]="ST300MM0006 " [12]="ST300MM0006 ")'
ST300MM0006 
1. ST900MM0006 [13]
2. ST900MM0006 [14]
declare -a productids='([0]="ST900MM0006 " [1]="ST600MM0006 " [2]="ST600MM0006 " [3]="ST600MM0006 " [4]="ST600MM0006 " [5]="ST600MM0006 " [6]="ST600MM0006 " [7]="ST600MM0006 " [8]="ST300MM0006 " [9]="ST300MM0006 " [10]="ST300MM0006 " [11]="ST300MM0006 " [12]="ST300MM0006 " [13]="ST300MM0006 ")'

CSV中的列如下所示:column

我在这个相同的脚本中使用了这种在bash中追加数组的方法,它工作正常,有人可以帮我弄清楚为什么这不起作用?我尝试过productids + =“$ productid”,并且出于某种原因,只需将所有内容附加到数组中第一个条目的末尾。

迭代数组:

for i in ${productids[@]}
do
    echo "${productids[i]}"
done

完整输出:

ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006

2 个答案:

答案 0 :(得分:2)

您可以像这样轻松追加到Bash数组:

productids+=("$productid")

您会在Bash Guide arrays上找到类似的示例。

答案 1 :(得分:0)

我在迭代时错误地访问了数组。应该使用&#34; $ i&#34;而不是&#34; $ {productids [i]}&#34;。