LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-29-2014, 09:14 AM   #1
jdveencamp
LQ Newbie
 
Registered: May 2014
Posts: 3

Rep: Reputation: Disabled
Shell envvar question


Hello, I'm looking for shell advice. (Hopefully I got the right forum)

I have a block of code we put into many scripts, and it gets a list of hosts to execute commands against. For readability purposes I'd like the two envvars the scripts uses to be at the front and one within the other. But the 2nd variable isn't resolved yet. I'd like the shell to handle it for me, but I don't have the syntax right. Is there a way to code this?

This doesn't work:
matchTag='test_x86'
command='/opt/local/bin/dosomething.sh $theHost'
#
hostFile='/opt/local/bin/linux/@Linux_hosts.txt'
calling_script=`basename $0` ;
grep $matchTag $hostFile | grep -v '#' | cut -f 1 -d : > /tmp/$calling_script.txt
while read -u 3 theHost ; do $command ; done 3< /tmp/$calling_script.txt

I could do this, but it's not as maintenance friendly.
matchTag='test_x86'
command='/opt/local/bin/dosomething.sh'
#
hostFile='/opt/local/bin/linux/@Linux_hosts.txt'
calling_script=`basename $0` ;
grep $matchTag $hostFile | grep -v '#' | cut -f 1 -d : > /tmp/$calling_script.txt
while read -u 3 theHost ; do $command $theHost; done 3< /tmp/$calling_script.txt

Sometimes I'll want the dynamically retrieved $theHost at the end of $command, sometimes in the middle of $command, etc. So if I could code the not yet resolved envvar into $command and have the shell handle it at runtime, it would be ideal.

Any ideas? Can I put some quotes, double quotes, brackets, curlies or some other special character around things and get the shell to do my work?

Thanks!
Jon
 
Old 05-29-2014, 10:49 AM   #2
potato_farmer
Member
 
Registered: May 2014
Posts: 55

Rep: Reputation: Disabled
try:
command="/opt/local/bin/dosomething.sh \$theHost"

and call it later using:
do eval $command
 
Old 05-29-2014, 10:53 AM   #3
jdveencamp
LQ Newbie
 
Registered: May 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Works perfectly with your tweak.
Much appreciated.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell question KMcKee Linux - Newbie 4 10-23-2012 06:34 PM
A question about shell (bash shell) thomas2004ch Linux - Newbie 4 08-27-2009 02:09 AM
C-shell question vl.yc.lw Programming 2 12-14-2008 01:32 AM
shell question Rob Pijpers Linux - Newbie 4 08-16-2006 02:12 AM
c(++) shell question karlan Programming 3 09-14-2003 04:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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