Skip to main content
added 27 characters in body
Source Link
gj.
  • 11
  • 3

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

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=","

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

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

Source Link
gj.
  • 11
  • 3

bash: how to pass variables to a loop within a process substitution

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=","

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