LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-18-2009, 03:16 PM   #1
JDska55
LQ Newbie
 
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Rep: Reputation: 15
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
 
Old 06-18-2009, 03:31 PM   #2
alinas
Member
 
Registered: Apr 2002
Location: UK, Sywell, EGBK
Distribution: RHEL, SuSE, CentOS, Debian, Ubuntu
Posts: 60

Rep: Reputation: 20
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.
 
Old 06-18-2009, 03:33 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 06-18-2009, 04:01 PM   #4
JDska55
LQ Newbie
 
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Original Poster
Rep: Reputation: 15
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.

Last edited by JDska55; 06-18-2009 at 04:05 PM.
 
Old 06-18-2009, 04:28 PM   #5
alinas
Member
 
Registered: Apr 2002
Location: UK, Sywell, EGBK
Distribution: RHEL, SuSE, CentOS, Debian, Ubuntu
Posts: 60

Rep: Reputation: 20
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.
 
Old 06-18-2009, 04:28 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 04:32 PM.
 
Old 06-18-2009, 04:37 PM   #7
JDska55
LQ Newbie
 
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Original Poster
Rep: Reputation: 15
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Assigning variables and string parsing in bash JDska55 Linux - Newbie 9 06-16-2009 11:51 AM
Assigning variables by awk(?) bioinformatics_guy Linux - Newbie 2 02-19-2009 01:01 PM
Bash - Getting a for loop to process variables with a space in them davee Linux - General 4 10-08-2005 12:21 PM
BASH variables getting unset outside of loop trevelluk Programming 2 03-25-2005 08:14 AM
C++ Assigning attributes to variables ? xconspirisist Programming 16 11-05-2003 07:08 AM


All times are GMT -5. The time now is 03:36 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration