Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
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.
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[@]}"
Last edited by David the H.; 06-18-2009 at 03:32 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.