0

I have written the following script ,

#!/bin/bash
My_File=Image.csv
Value=YES
cat "$My_File" | while IFS=, read first last
do
    echo "first='$first' last='$last'"
    if [ "$last" == "$Value" ];
       then
       echo Match_Found
       echo $first
       array+=("$first")
       echo $first is Added
    fi
done
echo (${#array[@]})

It dosenot add any value to the array , Could someone point out to the issue . The Input is as follow ,

FA_2015-01_666,NO
FA_2015-01_777,YES
FA_2015-01_888,NO
FA_2015-01_999,YES
FA_2015-01_555,YES
9
  • array+={"$first") what happens when you change the last ) to a } ? Commented Jul 10, 2015 at 9:46
  • 2
    Better yet, change the { to a (. Commented Jul 10, 2015 at 9:47
  • array+=($first) - This gaives same result , 0 and so does this array+=("$first") Commented Jul 10, 2015 at 9:51
  • What i want to know is why it even runs with unmatched braces Commented Jul 10, 2015 at 9:52
  • Actually that was an error when i copied over the code - My mistake ,on checking the Terminal , it appears to be correct Commented Jul 10, 2015 at 9:53

2 Answers 2

2

Redirect the file in, don't pipe from cat or the loop is run in a subshell and everything in it is lost when it ends.

#!/bin/bash
My_File="Image.csv"
Value=YES
while IFS=, read first last
do
    if [ "$last" == "$Value" ];
       then
       echo Match_Found
       echo $first
       array+=("$first")
       echo $first is Added
    fi
done < "$My_File"
echo "${#array[@]}"
Sign up to request clarification or add additional context in comments.

2 Comments

This works perfect , i was not aware of the cons mentioned by you.Thanks a lot for quick debug
@AsheshNair No Problem. Just remember any variables set in a pipeline are lost when it ends :)
0

I haven't done some bash coding in some time, but it should be like this (The way i do it):

#our table to insert an item to
table=( )
tableLength=${#table[@]}

#inserting items
table[$tableLength]="foo"
#you have to refresh this variable everytime
tableLength=${#table[@]}
table[$tableLength]="bar"
#or you can do this without refreshing the length variable
table[${#table[@]}]="foobar"

I recommend you use:

table=()
table[${#table[@]}]="item!"

using # as the length, and table[@] to display all items, so it gets the length of the array and sets an item to the length of an array or table

11 Comments

also tables and arrays are the same thing
This is tangibly related to the question and doesn't provide and answer to what OP is actually asking.
alright, what's the fun in coding if you just copy/paste the answers, this is the way i learned, so I thought it would help.
Who's copy and pasted anything ? I think the fun is actually solving problems instead of posting slightly related, personal preferences for coding. I honestly don't understand what your answer is trying to achieve.
Well if you have actually read the answer instead of downvoting, the comments are pretty clear to me, I'm no scientist but I think i know what #inserting items means.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.