LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2008, 02:41 PM   #1
elinenbe
LQ Newbie
 
Registered: Oct 2007
Posts: 23

Rep: Reputation: 15
Grab a value from a variable (bash)?


I could use a bit of bash help -- not even sure if this is possible...

In the beginning of my script, I assign a couple of variables:
Code:
emails_eric="eric@jj.com, ericl@mrpibb.com, eric_l@work.com"
emails_mark="mark@mark.com, mark@work.com, mark@home.com"
emails_dave="dave@mrd.com, dave@school.edu, dave@none.com"
then I do something like
Code:
for user in eric mark dave
   echo $user
   ... do stuff with name user ...
   
done
Now, in the section do stuff with name user I'd love to use the for user loop to grab the emails from above. I can't figure this out:

I've tried stuff like
Code:
user_email="emails_"$user
echo $`echo $user_email`
What I would like to do is grab the values from the top of my script
So I could do something like "email emails_user", and it will use the list from above depending on the person.

Thanks,
Eric
 
Old 04-01-2008, 03:00 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Prolly should be simpler than this but something like 'for user in eric mark dave; do eval echo \${emails_${user}}; done'.
 
Old 04-01-2008, 03:13 PM   #3
elinenbe
LQ Newbie
 
Registered: Oct 2007
Posts: 23

Original Poster
Rep: Reputation: 15
Thanks. I didn't know about eval. Sure cleans up my mess pretty nicely. Thanks!
 
Old 04-01-2008, 03:48 PM   #4
elinenbe
LQ Newbie
 
Registered: Oct 2007
Posts: 23

Original Poster
Rep: Reputation: 15
How can I take the output of the eval and store it to a variable?

I'd love to do:

myemails =`eval echo \${emails_${user}}`
echo $myemails

but that's not working here. Any ideas?

Thanks,
Eric
 
Old 04-01-2008, 03:50 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by elinenbe View Post
Thanks. I didn't know about eval. Sure cleans up my mess pretty nicely.
Heh. NP. Here's some if you haven't read 'em:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 04-01-2008, 04:01 PM   #6
elinenbe
LQ Newbie
 
Registered: Oct 2007
Posts: 23

Original Poster
Rep: Reputation: 15
I just had it out of order... figured it out. Thanks.

This worked:
Code:
eval myemails=\${emails_${user}}
echo $myemails
 
  


Reply



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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Bash and ip variable skids48 Linux - Networking 2 02-15-2008 07:02 AM
grab FTP output in bash script bokini Linux - General 2 02-03-2006 02:11 PM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 04:38 PM
grab - Simply a useless bash script to take a screen shot; Required command: import micxz Programming 2 10-08-2003 07:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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