LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2011, 12:53 PM   #1
fur
Member
 
Registered: Dec 2003
Distribution: Debian, FreeBSD
Posts: 310

Rep: Reputation: 35
Bash for loop use variable name as an array


Writing a script where I have several arrays defined and each array contain a list of servers.

array names,
T1 T2 T3 T4 T5

I want to have a for loop go though another for loop containing the array names, then perform actions based on the contects of each one.


Tried this, but doesn't work.

Code:
for ARRAY in T1 T2 T3 T4 T5; do
        for SERV in ${$ARRAY[@]}; do
                doing stuff...
                doing stuff...
        done
done
I would expect using ${$ARRAY[@]} would cause the "for SERV" loop to go through each arrays values.


gives an error like this,

line 117: ${$ARRAY[@]}: bad substitution

Can you use a variable name when referencing an array is my question.
 
Old 11-03-2011, 01:52 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Untested: ${${ARRAY}[0]}
 
Old 11-03-2011, 02:39 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Untested:
Code:
for ARRAY in T1 T2 T3 T4 T5; do
        for SERV in $( eval '"${'$ARRAY'[@]}"' ); do
                doing stuff...
                doing stuff...
        done
done
eval is dangerous when the following string contents are uncontrolled but in this case ...
 
Old 11-03-2011, 02:43 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
No, that won't work. Not without using eval, which is NOT recommended.

http://mywiki.wooledge.org/BashFAQ/048

This is what's known as indirect referencing.

http://mywiki.wooledge.org/BashFAQ/006

There are probably several ways to do what you want, with associative arrays being the best bet.

Edit: simple example:
Code:
#!/bin/bash

declare -A T1 T2 T3 T4 T5

T1=( [s1]=server1 [s2]=server2 [s3]=server3 )
T2=( [s1]=server1 [s2]=server2 [s3]=server3 )
T3=( [s1]=server1 [s2]=server2 [s3]=server3 )
T4=( [s1]=server1 [s2]=server2 [s3]=server3 )
T5=( [s1]=server1 [s2]=server2 [s3]=server3 )

for i in s1 s2 s3; do

     echo "T1: ${T1[$i]} T2: ${T2[$i]} T3: ${T3[$i]} T4: ${T4[$i]} T5: ${T5[$i]}"

done

Last edited by David the H.; 11-03-2011 at 02:55 PM.
 
Old 11-03-2011, 02:51 PM   #5
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
See my answer here, in the function (named "swap") that takes an array name as a parameter.

In this situation you seem to control the input for eval, so:
Code:
#!/bin/bash
T1=( server1 server2 server3 )
T2=( server4 server5 server6 )
T3=( server7 server8 server9 )
T4=( server10 server11 server10 )
T5=( server13 server14 server15 )

for ARRAYNAME in T1 T2 T3 T4 T5; do
    eval "THE_ARRAY=\${$ARRAYNAME[@]}"
    for SERV in ${THE_ARRAY[@]}; do
        echo -n "$SERV "
    done
    echo
done
But beware when you use eval, test it very thorougly. It's very easy for it to go out of control, even worst: it's very easy for you to *think* it won't go out of control. So again *think carefully* and if at all possible use another solution.

Last edited by Juako; 11-03-2011 at 03:11 PM.
 
  


Reply



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
Why Perl's foreach loop can't use an array element as the control variable? password636 Programming 1 07-22-2011 09:58 AM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
simple BASH while loop question / Array riotxix Programming 10 01-03-2010 08:57 PM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM
bash - loop over variable array names talanis Programming 2 02-19-2009 11:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:05 PM.

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