LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   String Evaluation Not Working (https://www.linuxquestions.org/questions/linux-newbie-8/string-evaluation-not-working-4175716002/)

ds89 08-23-2022 04:05 PM

String Evaluation Not Working
 
I have the following code.

#! usr/bin/bash

schemaPrefix=("aos")
tables_aos=("A" "B" "C")

for j in "${!schemaPrefix[@]}"; do

aschema=${schemaPrefix[j]}
schema=$aschema
eval tables=tables_$aschema
echo ${tables[@]}
done

The code should display:
A B C
However, it displays:
tables_aos

I have several lists. They are named as "table" followed by underscore and a suffix. For example, table_aos, table_apd, etc. I want to loop through all those lists and print the content of each list.

Keith Hedger 08-23-2022 05:38 PM

try:
Code:

#!/bin/bash
 
suffix="aos"
tables_aos=("A" "B" "C")
echo table data = ${tables_aos[@]}
name=tables_${suffix}[@]
echo name of table = $name
data=${!name}
echo all the data =  ${data[@]}
 
echo individual data:
for arg in ${data[@]}; do
echo $arg
done

gives:
Code:

table data = A B C
name of table = tables_aos[@]
all the data = A B C
individual data:
A
B
C

your #! is wrong it should be an absolute path and BASH is usually in /bin/bash rather than /usr/bin/bash
!is redirect and you should try to avoid eval where possible
USE CODE TAGS!


All times are GMT -5. The time now is 03:32 AM.