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 Cheers, Jarrod |
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. |
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}' All you need is Code:
for address in $(ec2din --simple | awk '$2 ~ /running/ {print $3}'); Cheers, Tink |
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 Code:
Mercury |
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. |
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 |
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. |