LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-23-2022, 04:05 PM   #1
ds89
LQ Newbie
 
Registered: Aug 2022
Posts: 1

Rep: Reputation: 0
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.
 
Old 08-23-2022, 05:38 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,137

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
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!
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capture 1000 lines before a string match and 1000 line a string match including line of string match ? sysmicuser Linux - Newbie 12 11-14-2017 05:21 AM
evaluation of postfix string using bash shell kakalig2007 Linux - Newbie 1 03-15-2011 01:39 AM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
complete string conversion - without parts evaluation slackie1000 Programming 9 01-06-2004 01:10 PM
string evaluation: FORTRAN OR C .. slackie1000 Programming 3 12-23-2003 10:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration