Creating bash array name from variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating bash array name from variable
# 1  
Old 02-27-2012
Creating bash array name from variable

Hi gurus,

I need to create arrays from variables, via a loop.

The issue I have is with the array name creation. How do I use a variable to define an array?

I want to do something like
declare -a $H
where $H is my loop variable.

I then need to add items to each array I've created, something like this
$H=( ${$H[@]} "$I")

$I is the variable I'm using to loop in the array items.

I'm guessing that I'll need to use eval but nothing I've tried works. Escaping non-printable characters doesn't work either.

I can define an empty array using the $H variable, but I'm unable to add items to it or print out the full array item list.
I have no problems when defining a non-variable array name, but I need to create over 100 lookup tables, with number of items ranging from 1 to 10 -15 for each array, all from loop.

Any suggestions would be most welcolmed
# 2  
Old 02-27-2012
Code:
H[${#H[*]}]=$I

Guru.
# 3  
Old 02-27-2012
Thanks Guru, but not sure if this is what I'm after.

example of my code is:
Quote:
for H in "list of items"
do declare -ax $H Setting the array name
for I in "list of array items"
do "$H"=( "${$H[@]}" "$I" ) adding array items to required arrays
echo ${"$H"[@]} print all items in the array just added to
done
done
e.g. Array name (H) could contain a persons name. The items (I) in the array could include things like address, phone number, D.O.B.

So I need to use a for loop to create the initial arrays (persons name), then use another for loop to populate the arrays with specific items. Then I need to be able to print out specific items or the entire array.
The issue looks to be with my variable expansion, but I could be wrong.

Does this make better sense?

Last edited by melias; 02-27-2012 at 11:21 PM..
# 4  
Old 02-27-2012
Perhaps this might help:

Code:
H=boats
eval $H'[1]="Testing"'
eval echo '${'$H'[1]}'
echo ${boats[1]}

Thing is, that eval ends up running a sub shell so it probably won't be real fast to assign 500 arrays of 10-15 items each.



Another thing you could try and do is write out a whole head of assign statements to a file and then source it into the current shell

Code:
H=fish
echo $H'[1]="carp"' > /tmp/asgn_$$
echo $H'[2]="cod"' >> /tmp/asgn_$$
echo $H'[3]="marlin"' >> /tmp/asgn_$$
 
. /tmp/asgn_$$
rm -f /tmp/asgn_$$
 
echo ${fish[2]}


Last edited by Chubler_XL; 02-27-2012 at 11:56 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

2. Shell Programming and Scripting

Adding an element to a bash array with a variable

Hello, I have a simple task and I am having some trouble with the syntax. I have a variable with an assigned value, CMD_STRING='-L 22 -s 0 -r -O -A i -N 100 -n' I would like to add that variable to an array. As far as I have been able to look up, the syntax should be something like, ... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

3. Shell Programming and Scripting

Bash question: working with an array of previously set variable strings

while i've used arrays to work with variables, i've never used them to loop through a set of strings and wanted to ask the community for some feedback or assistance. let me be specific. here's my code: # URL port Variables port2195=`nc -z $url2195 2195` port2196=`nc -z $url2196 2196`... (5 Replies)
Discussion started by: hungryd
5 Replies

4. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

5. Shell Programming and Scripting

Creating variable using awk in bash

I would like to create a variable within my bash script using awk. I'm reading in a line from an external file, then outputting to a new file in a specific format. But, it doesnt quite work as I have expected and could use some help. (A pertinent excerpt of ) the bash code is: count=1 ... (4 Replies)
Discussion started by: snoitacilppa
4 Replies

6. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies

7. Shell Programming and Scripting

creating variable array name

#!/bin/ksh #export CLASSPATH=$CLASSPATH:~dialp/cso/classes:/opt/oracle/product/8.1.6/jdbc/lib/classes12.zip #export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/oracle/product/8.1.6/lib DATE="`date '+%m%d%Y'`" PATH=.:$PATH export PATH town_name='123' town_name='123' town_name='345'... (1 Reply)
Discussion started by: priyanka3006
1 Replies

8. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies

9. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

10. Shell Programming and Scripting

creating array variable

Hi all, i am quite fimiliar with shell scripting but i wouldn't regard myself as a semi professional at it. I am trying to create an array variable to read in 4 lines from a file using head and tail command in a pipeline and store each line into each array. I have done the scripting in unix... (2 Replies)
Discussion started by: scriptingmani
2 Replies
Login or Register to Ask a Question