I'll appreciate your help with the following issue:
I'm trying to set an array which contains a variable as part of the array name, example: Arr_$COUNTER
(where $COUNTER
is changed based on a loop count)
Every possible way I have tried came up with an error, such as "bad substitution" or "syntax error near unexpected token"
Here is the entire flow:
There is one file which contain multiple lines. each line has 6 values separated by space
10 20 30 40 50 60 100 200 300 400 500 600
The script, is meant to read each line from the file, and declare it as an array (with the line number which is the variable.
as a test, each value should be printed and eventually another function will be executed on each value.
#!/bin/bash COUNTER=1 LINES=`wc -l VALUES_FILE.txt | awk '{print $1}'` echo "Total number of lines "$LINES echo while [ $COUNTER -le $LINES ] do echo "Counter value is $COUNTER" field=`awk "NR == $COUNTER" VALUES_FILE.txt` echo "Field = $field" declare -a "arr$COUNTER=($field)" echo "arr$COUNTER[0] = ${arr$COUNTER[0]}" echo "arr$COUNTER[1] = ${arr$COUNTER[1]}" echo "arr$COUNTER[2] = ${arr$COUNTER[2]}" echo "arr$COUNTER[3] = ${arr$COUNTER[3]}" echo "arr$COUNTER[4] = ${arr$COUNTER[4]}" echo "arr$COUNTER[5] = ${arr$COUNTER[5]}" let COUNTER=COUNTER+1 echo done echo "The End" echo
Here is the result:
Total number of lines 2 Counter value is 1 Field = 10 20 30 40 50 60 ./sort.sh: line 12: arr$COUNTER[0] = ${arr$COUNTER[0]}: bad substitution The End
What should be changed / fixed in order to have it working properly?
thank !