0

I'm pretty new to bash.

I have the following code:

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

Which produces the following result:

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 ")'

The column from the CSV looks like this: column

I've used this method of appending to arrays in bash in this same script and it worked fine, can someone help me figure out why this isn't working? I tried doing productids+="$productid" and that, for some reason, just appends everything to the end of the first entry in the array.

Iterating through the array:

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

Full output of this:

ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006 
ST900MM0006
Noah
  • 1
  • 2
  • 1
    So .. what exactly isn't working? Your results look exactly like what I would expect from your code. What were *you* expecting? – ghoti Jun 05 '17 at 14:50
  • Oh (re: second edit): You need more quotes. `for i in "${productids[@]}"`. – Charles Duffy Jun 05 '17 at 14:52
  • When I iterate through the array with the code I just added to the main post, I just get the first value repeating. – Noah Jun 05 '17 at 14:52
  • Well, yeah, you're appending the same `"$productid"` more than once, so *of course* it's the same value. If you appended two different values, you'd have two different items in the results. – Charles Duffy Jun 05 '17 at 14:53
  • @CharlesDuffy Just tried adding those quotes and still just got the first value repeating. – Noah Jun 05 '17 at 14:53
  • Right, the quotes solve a different problem (you hadn't yet specified "repeating" when I added that). The values repeating are because you're adding identical values over and over, so **of course** they repeat. – Charles Duffy Jun 05 '17 at 14:54
  • @CharlesDuffy At some point I'm hypothetically appending ST300MM0006, but that never shows up when I iterate through and echo the results. – Noah Jun 05 '17 at 14:54
  • 3
    Well, stop doing things "hypothetically" and provide a [mcve] that actually shows your **real** problem. – Charles Duffy Jun 05 '17 at 14:55
  • @CharlesDuffy Sorry, I just added the output of the iteration. Is it not correct that line three of the first block of code should be adding ST300MM0006 to the array? Which it is, I guess, I can see that in the declare. So how come that never shows up when I iterate through in the for loop? – Noah Jun 05 '17 at 14:56
  • f/e, when you provide "full output", it should be full output of the **exact code in the question**. Which, right now, it's not (since the exact code in the question only does two appends). Edit your question so your code and your sample data match, and also add *expected* output and draw out how it's different from *actual* output. – Charles Duffy Jun 05 '17 at 14:57
  • As for "line three of the first block of code", it just adds `$productid` again. Nothing in the code you're providing here *sets* a value for `productid` at all, making it insufficiently complete as to be verifiable: Someone else can't copy-and-paste it and get an identical result. – Charles Duffy Jun 05 '17 at 14:57
  • 2
    BTW, we have real edit history here. You don't need to add things as EDIT1/EDIT2/EDIT3 at the end -- form your edits for maximum readability for someone who's never seen the question before; someone coming from a prior version who wants to know what changed can just check the diff. – Charles Duffy Jun 05 '17 at 14:58
  • @CharlesDuffy I edited the post with the full relevant code and output. But you can obviously see what I'm talking about, you can see that $productid is set at ST300MM0006 before it's added to the array and that the array has that value in it but the manner in which I'm iterating through the array is not producing that result. – Noah Jun 05 '17 at 15:07
  • 1
    I didn't ask for "full relevant code". I asked for (and the site rules call for you to provide) a [mcve] -- the **shortest possible code** that produces the same bug when run on its own with no unnecessary environment setup or dependencies. – Charles Duffy Jun 05 '17 at 15:08
  • @CharlesDuffy Did I catch you at a bad time or something? I don't see how it's possible that you don't know what I'm asking at this point. I realize that my problem is probably trivial, but I don't know what more information I could possibly give you to help you answer the question. – Noah Jun 05 '17 at 15:11
  • Also, make sure that you test your code *exactly as given in the question*. If there's a bunch of stuff between point A and point B in the real code that you think is irrelevant so you're taking it out? Make sure you still have the problem when that irrelevant stuff is taken out. – Charles Duffy Jun 05 '17 at 15:11
  • 1
    Not a bad time, particularly. I expect that attempts to produce a single, short, self-contained, copy/pasteable script will lead you to discover the cause of the problem yourself. And if it *doesn't*, it'll put the problem into a state where I can actually build an answer for it. – Charles Duffy Jun 05 '17 at 15:12
  • @CharlesDuffy Okay, I concede, that helped me figure it out. Sorry for the frustration and thanks! – Noah Jun 05 '17 at 15:23

2 Answers2

2

You can append to a Bash array easily like this:

productids+=("$productid")

You'll find similar examples in the Bash Guide on arrays.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
janos
  • 120,954
  • 29
  • 226
  • 236
0

I was accessing the array incorrectly when iterating through it. Should have used "$i" instead of "${productids[i]}".

Noah
  • 1
  • 2
  • Perhaps. When you ran `for i in ${productids[@]}` you were stepping through the values. If you wanted `$i` to be an index, you should have used `for i in "${!productids[@]}"` instead. – ghoti Jun 05 '17 at 15:55