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
$csvfile
is undefined. Can you edit your example to show how$csvfile
is defined? – Jim L. Aug 20 '19 at 20:01${linedata[2]}
is actually a space character? – FelixJN Aug 20 '19 at 20:17awk
- this seems to be a simpler (and faster) tool for this problem. – FelixJN Aug 20 '19 at 21:47But, 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
– gj. Aug 21 '19 at 15:27if
condition/block, e .g."_ ${linedata[2]}_"
(the underscore = whitespace), it works! I don't need to do that outside of theif
block. Very confusing. Would someone elucidate me? Thanks for all the replies.