1

Inside the second for loop I am getting the error '${Data${c}Answer[*]}: bad substitution'. I believe it's the $c. Still new to bash script so might be an easy fix but can't seems to figure it out.

declare -a Data1Answer=('0' '3' '4' '6' '9')
declare -a Data2Answer=('5' '7' '9' '11' '13')
for c in {1..2}; do
    echo \$c
    t=0
    java \${prog1} < data\${c}.txt &> user.out
    for ans in \${Data\${c}Answer[*]}; do
        if grep -q \$ans user.out
        then
            ((t++))
        fi
    done
Toto
  • 89,455
  • 62
  • 89
  • 125
lag
  • 520
  • 2
  • 10
  • 26

1 Answers1

2
declare -a Data1Answer=('0' '3' '4' '6' '9')
declare -a Data2Answer=('5' '7' '9' '11' '13')

for c in {1..2}; do
    echo $c
    t=0
    java "$prog1" < "data${c}.txt" &> user.out

    # a nameref
    declare -n data=Data${c}Answer

    for ans in "${data[@]}"; do
        grep -q "$ans" user.out && ((t++))
    done
    echo $t
done

Notes:

  • ${braces} are not the same as "$quotes"
  • to iterate over the elements of an array, use "${array[@]}" with the quotes and the @ index (selfserving explanation).
glenn jackman
  • 238,783
  • 38
  • 220
  • 352