LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-15-2009, 11:38 AM   #1
JDska55
LQ Newbie
 
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Rep: Reputation: 15
Assigning variables and string parsing in bash


Hey all-
I've been working with Amazon's EC2 platform and I'm trying to automate some of the grunt work in a bash script. The problem is, though, that I don't know how to assign variables that I need to access the instances running in the cloud. Here's an example:

Code:
ec2din --simple
i-91a841be     running ec2.000.000.00.0.amazonaws.com
This command is describing an instance with an ID, its status, and the public DNS address. I need to be able to assign the DNS address to variable or something so I can use it in an ssh command to connect to it, like this:

ssh -i cert.pem root@[VARIABLE]

is there any way to do this or something similar in bash? If you have other methods I'd be glad to hear them as well. I really don't want to try to pick up Python or Ruby to do this. Thanks in advance!

Jarrod
 
Old 06-15-2009, 11:46 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You can try with the read statement:
Code:
while read id status address
do
  echo $id
  echo $status
  echo $address
  ssh -i cert.pem root@${address}
done < <(ec2din --simple)
provided that the line you posted is the only output from the ec2din command.
 
Old 06-15-2009, 11:46 AM   #3
nuwen52
Member
 
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
Try:
VARIABLE=`ec2din --simple | cut -d " " -f3`

Note, there is a space between the quotes.
 
Old 06-15-2009, 12:03 PM   #4
JDska55
LQ Newbie
 
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by nuwen52 View Post
Try:
VARIABLE=`ec2din --simple | cut -d " " -f3`

Note, there is a space between the quotes.
I think this just assigns 'ec2din --simple | cut -d " " -f3' to VARIABLE. typing echo $VARIABLE just returns the command, not the output. I'll try the do while loop, but there's a new wrinkle thrown in too now. When more than one instance is started, it lists them all on separate lines, and terminated instances show up as:
InstanceID Terminated
Thoughts on how to get one specific address out from a list like that?
 
Old 06-15-2009, 12:16 PM   #5
nuwen52
Member
 
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
The quotes around the command are backquotes, not the normal quotes.
backquote=quotemark on the key of the "~" (tilde?)

Also, post the new output with the terminated lines please?

Last edited by nuwen52; 06-15-2009 at 12:20 PM.
 
Old 06-15-2009, 12:34 PM   #6
JDska55
LQ Newbie
 
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Original Poster
Rep: Reputation: 15
The new output looks like this:

Code:
i-481as59a     terminated
i-3b48ah56     running ec2.172.161.22.14@amazonaws.com
i-37547ah3     running ec2.184.113.142.10@amazonaws.com
I don't think the terminated ones will ever be below the running ones. Pending ones might be, but as long as I wait long enough the 'pending' option should never show up.

Ah, I didn't notice the backquotes. With those,
VARIABLE turns to: i-47ab529 terminated i-3b48ah56 ec2.####.amazonaws.com i-37547ah3 ec2.####.amazonaws.com
 
Old 06-15-2009, 12:47 PM   #7
nuwen52
Member
 
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
sending the output before the cut to a "grep -v terminated" will get rid of the terminated lines. What do you know about the instance you are trying to get? If you know the ID, you can replace the grep -v terminated with a grep $ID and that will get you one line.
 
Old 06-15-2009, 01:06 PM   #8
JDska55
LQ Newbie
 
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Original Poster
Rep: Reputation: 15
the grep -v line worked great, but no, unfortunately, that's one of the reasons I'm having issues with this. There's no way to specify or know what an instance's ID is without pinging it with this command. As far as I know, there's not a tool that returns only the address or instance ID.
 
Old 06-15-2009, 01:18 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Code:
for address in $(ec2din --simple | awk '$2 ~ /running/ {print $3}');
do
  ssh -i cert.pem root@${address}
done
 
Old 06-16-2009, 11:51 AM   #10
JDska55
LQ Newbie
 
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Original Poster
Rep: Reputation: 15
Code:
for address in $(ec2din --simple | awk '$2 ~ /running/ {print $3}');
do
  ssh -i cert.pem root@${address}
done
Simply magic.
Thanks!
 
  


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
how to stop parsing shell variables in bash script inside cat area? jackandking Linux - Newbie 2 03-10-2009 07:44 AM
Assigning variables by awk(?) bioinformatics_guy Linux - Newbie 2 02-19-2009 01:01 PM
Programming in BASH - Parsing a String Mistro116@yahoo.com Programming 9 06-18-2008 07:43 PM
bash - pull out variables from a | deliminated string elinenbe Programming 11 02-16-2008 12:36 AM
parsing a string from grep output in bash xpromisex Programming 2 11-12-2006 10:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:45 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