LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell scripting array question (https://www.linuxquestions.org/questions/programming-9/shell-scripting-array-question-734149/)

ulver 06-19-2009 06:04 AM

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

akiku 06-19-2009 06:16 AM

You may want to check the Advanced Bash-Scripting Guide

colucix 06-19-2009 06:43 AM

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.

ulver 06-19-2009 06:50 AM

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
:(

colucix 06-19-2009 07:05 AM

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.

sundialsvcs 06-19-2009 07:09 AM

(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?"


All times are GMT -5. The time now is 11:29 AM.