LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Assigning variables in a bash for loop (https://www.linuxquestions.org/questions/linux-newbie-8/assigning-variables-in-a-bash-for-loop-733969/)

JDska55 06-18-2009 02:16 PM

Assigning variables in a bash for loop
 
Hey all-
I'm running a for loop in bash that grabs the DNS address of a remote server. Most of the time, there is more than one running. I want to write a for loop that will cycle through and assign the DNS addresses to variable names that can be called later in the script. Here's what I've got so far:
Code:

count=0
for address in $(ec2din --simple | awk '$2 ~ /running/ {print $3}');
do
  count=`expr $count + 1`
  echo $count
  address"$count"="$address" #This line is the meat of my question
  rsync -e "ssh -i `echo ~`/.ec2/gsg-keypair" -avz /home/admin/data root@${address}:/root
  scp -i ~/.ec2/gsg-keypair ~/Kai-script root@${address}:/root
done

This runs fine, but when I try to call one of the values $address(insertnumberhere), it just comes up as blank on the terminal and doesn't evaluate to anything in a call. In addition, the echo in line 5 also is blank, even though I know it has a value. I've tried a few different varieties of statements in the line in question... any ideas why this isn't assigning correctly?

Cheers,
Jarrod

alinas 06-18-2009 02:31 PM

address"$count"="$address" #This line is the meat of my question

In your line above isn't address a variable? Should it have $ in front of it?

Also, try ((count++)) notation. I try to avoid `expr $x + 1` notation.

Tinkster 06-18-2009 02:33 PM

You need to read the bash beginners tutorial and the bash
advanced scripting guide my friend. address in the for
loop is *not* an array variable; the for loop will assign
the resulting values from your
Code:

ec2din --simple | awk '$2 ~ /running/ {print $3}'
one at a time, and repeat the loop for them one by one.


All you need is
Code:

for address in $(ec2din --simple | awk '$2 ~ /running/ {print $3}');
do
  rsync -e "ssh -i `echo ~`/.ec2/gsg-keypair" -avz /home/admin/data root@${address}:/root
  scp -i ~/.ec2/gsg-keypair ~/Kai-script root@${address}:/root
done



Cheers,
Tink

JDska55 06-18-2009 03:01 PM

I've been reading them, Tink, and they do clarify, but even in their example I get the same issue. It's trying to evaluate the variable setting as a bash command, rather than just setting an environment variable. Perhaps I wasn't clear, I know how the for loop assigns the variables, the scp and rsync could basically not be there. I want to be able to address each instance separately and transfer different files, etc., later in the script *not in a for loop*. Here's the example script from the advanced bash scripting guide
Code:

#!/bin/bash
count=0
for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto
do
  count=`expr $count + 1`
  echo $planet
  PLANET"$count"=$planet
done
echo $PLANET1
echo $PLANET2

exit 0

yields this output:

Code:

Mercury
forloop: line 7: PLANET1=Mercury: command not found
Venus
forloop: line 7: PLANET2=Venus: command not found
Earth
forloop: line 7: PLANET3=Earth: command not found
Mars
forloop: line 7: PLANET4=Mars: command not found
Jupiter
forloop: line 7: PLANET5=Jupiter: command not found
Saturn
forloop: line 7: PLANET6=Saturn: command not found
Uranus
forloop: line 7: PLANET7=Uranus: command not found
Neptune
forloop: line 7: PLANET8=Neptune: command not found
Pluto
forloop: line 7: PLANET9=Pluto: command not found

So the expression is coming up correctly using the $planet, but it's not setting the variable like it should be. That's my specific hang up here.

alinas 06-18-2009 03:28 PM

One more attempt, now that I read your explanation again...

In the assignment:

address$count=$address

the shell has problems with having left and right side having substitution requests.

Try the following:

eval address$count=\$address

This forces a double scan.

David the H. 06-18-2009 03:28 PM

It might help if you try using a true array, rather than just a variable with a number attached. Also, choose a name for the array that's different from the for loop variable. Here's a simple example:

Code:

#!/bin/bash

count=0

for entry in Fry Leela Bender Nibbler; do

  array[$count]="$entry"
  ((count++))

done

echo "array entry 0 is: ${array[0]}"
echo "array entry 1 is: ${array[1]}"
echo "array entry 2 is: ${array[2]}"
echo "array entry 3 is: ${array[3]}"
echo "the total array is: ${array[@]}"


JDska55 06-18-2009 03:37 PM

Both of those options work great! The array might be a touch better just so they're all in one place, but that will be great. Thanks guys!


All times are GMT -5. The time now is 12:09 AM.