0

How do you pass variable values within a process substitution to a loop within that process substitution?

I'm reading a csv file into an associative array, and while reading each line, I would like to have an if loop to test that a particular element in my array is not empty (and do stuff for those):

IFS=","
$csvfile="path/to/file.csv"
declare -A arr1  
declare -A arr2  
declare -A arr3 

while read -r -a linedata
do
    arr1["${linedata[1]}"]="${linedata[10]}"
    arr3["${linedata[1]}"]="${linedata[0]}"

    echo repeat -"${linedata[2]}"-

    if [[ -n "${linedata[2]}" ]] && [[ ! "${linedata[2]}" == "" ]] ; then
        arr2["${linedata[2]}"]="${linedata[10]}"
        arr3["${linedata[2]}"]="${linedata[0]}"
    else
        echo skipped for "${linedata[2]}"
    fi
done < $csvfile

I see that although the echo right before the if block prints the value from the linedata array correctly, the values in the array do not even seem to get passed through to the if condition (the condition is never satisfied, although it should be so that the else block is always executed and there the array element value is blank).

I've tried doing < <($csvfile) but I got an error about permission denied.

Thanks in advance

  • 1
    $csvfile is undefined. Can you edit your example to show how $csvfile is defined? – Jim L. Aug 20 '19 at 20:01
  • Is the value truly blank or might it just be that an empty value in ${linedata[2]} is actually a space character? – Fiximan Aug 20 '19 at 20:17
  • 1
    Have you considered using awk - this seems to be a simpler (and faster) tool for this problem. – Fiximan Aug 20 '19 at 21:47
  • 1
    related: unix.stackexchange.com/questions/169716/… – cas Aug 21 '19 at 2:34
  • @Jim L., original post edited. @Fiximan, maybe you're right with awk -- just thought having an associative array (need to do some further processing) would make my life easier and I was not quite sure how to do that with awk. But, I have a fix that got it to work, though I have no clue why it works -- when I pad the quotes with a space when I access the hash array in the if condition/block, e .g. ` "_ ${linedata[2]}_" ` (the underscore = whitespace), it works! I don't need to do that outside of the if block. Very confusing. Would someone elucidate me? Thanks for all the replies. – gj. Aug 21 '19 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.