LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-19-2009, 06:04 AM   #1
ulver
LQ Newbie
 
Registered: Mar 2008
Posts: 14

Rep: Reputation: 0
Shell scripting array question


Hi, I made a little rsync script for log transfer.

Code:
SERVERS=(SERVER1 SERVER2 SERVER3)
SERVER1_SERV=(web ftp mail)
SERVER2_SERV=(web transcoding)
SERVER3_SERV=(web ftp mail)

for SERVER in ${SERVERS[@]}
do
        echo "Starting tranfer for server $SERVER"
        for SERVICE in ${$SERVER_$SERVICE[@]}
        do
                something_to_be_done

        fi
done
But when I run it I get ${$SERVER_$SERVICE[@]}: bad substitution

What am I doing wrong here :S
 
Old 06-19-2009, 06:16 AM   #2
akiku
Member
 
Registered: Jun 2009
Distribution: Slackware
Posts: 62

Rep: Reputation: 17
You may want to check the Advanced Bash-Scripting Guide
 
Old 06-19-2009, 06:43 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
for SERVICE in ${$SERVER_$SERVICE[@]}
This does not make sense. The SERVICE variable is the loop var, so you cannot use it to build the loop itself. Please, explain what you're trying to do exactly, since it is not clear from your script.
 
Old 06-19-2009, 06:50 AM   #4
ulver
LQ Newbie
 
Registered: Mar 2008
Posts: 14

Original Poster
Rep: Reputation: 0
I have got a array of servers and for each servers an array of services. Im trying to iterate through them so I can make rsync backup for each one. Hope you understand
I tried
Code:
for SERVER in ${SERVERS[@]}
do
        echo "Starting tranfer for server $SERVER"
       for ((i=0;i<$SERVER_$SERVICE;i++))
        echo ${SERVER}_${SERVICE}
        do
But it still doesnt work
 
Old 06-19-2009, 07:05 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Nope, I don't really understand. What do you expect the value of ${SERVER}_${SERVICE} should be?

I try to guess at this point: you have an array which store the names of the servers ($SERVERS) and three arrays which store the name of the services for each server ($SERVER1_SERV, $SERVER2_SERV, $SERVER3_SERV). So you want to loop over the servers names (SERVER1 SERVER2 SERVER3) to get the corresponding services names, right?

If this is the case, you have to use "indirect reference" to build the name of the three service variables and then loop over them:
Code:
#!/bin/bash
SERVERS=(SERVER1 SERVER2 SERVER3)
SERVER1_SERV=(web ftp mail)
SERVER2_SERV=(web transcoding)
SERVER3_SERV=(web ftp mail)
for SERVER in ${SERVERS[@]}
do
  echo "Starting tranfer for server $SERVER"
  for i in $(eval echo \${${SERVER}_SERV[@]})
  do
    echo $i
  done
done
Note the construct of eval echo \$. This is indirect variable reference.
 
Old 06-19-2009, 07:09 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
(Shrug...)

Why not use Perl? Or PHP? Ruby? Python?

You just have so many languages at your beck-and-call on a Linux system... that it makes very little sense to me to wrestle with "bash." You specify the language in the #! "shebang" line, and no one's the wiser.

Don't get me wrong: I've seen some amazing things being done with "bash," but to me it just always feels like, "well yes, I see that it works, but why bother?"
 
  


Reply

Tags
array, bash, scripting, shell



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
I have a shell scripting question bartock Linux - General 5 03-06-2008 02:46 PM
Bash Shell Scripting - using ls into array aliasofmike Programming 5 11-05-2007 03:00 PM
Shell scripting - Adding many numbers in an array Stingreen Linux - General 2 10-25-2004 05:25 PM
Shell Scripting Question Onyx^ Linux - General 5 04-27-2004 10:37 AM
Shell Scripting Question b_vasu Linux - Newbie 1 11-21-2003 02:10 PM

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

All times are GMT -5. The time now is 11:00 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