0

There is an array that is being fed into a for each loop that has a case statement within. The array is being generated through a read from CSV.

read -p "Path to CSV file with field data:" csvFile

while IFS=',' read -ra array; do

ar1+=("${array[0]}") # Field Name
ar2+=("${array[1]}") # Field ID
ar3+=("${array[2]}") # Field Type

done < $csvFile

arLength=${#ar1[@]}

        
for (( i=0; i<$arLength; i++ )); do

case ${ar3[$i]} in
    Drop-down)
        
    echo "foo"
    ;;

    Multi-select)
        
    echo "bar"
    ;;

    *)
        
    echo "none"
    ;;
esac
done

Values possible in ar3 include Multi-select, Multi-line, Drop-down, Text, Date, Numeric

The issue is that the values in the ar3 array are not being evaluated properly, and instead everything is receiving the else treatment (echo "none"). I have tried the following:

  • Case statement compares on one line Drop-down|Multi-select)
  • Various quotes (single and double) around either the array variable, options, or both
  • Escaping hyphens
  • Evaluating against another option w/o special characters

Not sure what I have wrong here to make it not evaluate properly.

smoooosher
  • 123
  • 5
  • 1
    you can add an `echo "${ar3[$i]}"` cmd in your "none" case, and check what was the value you get. I guess there could be some spaces in the array elements. – Kent Jun 21 '21 at 15:40
  • @Kent Forgot to add that I had done that as well. Also added a `printf %s "${ar3[$i]}" | od -vtc -to1` and it shows the following: 0000000 D r o p - d o w n \r 104 162 157 160 055 144 157 167 156 015 0000012 My goal with the above was to make sure that the array wasn't loading all values into a single array object (it doesn't - it prints correctly via `echo`) – smoooosher Jun 21 '21 at 15:55
  • 2
    The csv file uses DOS-style `\r\n` newlines, so the 3rd value is `Drop-down\r` which is not equal to "Drop-down". Fix with `dos2unix` or `sed 's/\r$//'` – glenn jackman Jun 21 '21 at 16:11
  • @glennjackman This was it! If you post as an answer, I'll mark it as such. Thanks! – smoooosher Jun 21 '21 at 16:20

0 Answers0