expand variable content within a command

expand variable content within a command

Post by Dundonal » Fri, 23 Jun 2006 19:27:55



I'm actually using a command within bladelogic called runcmd
(simultaneously fires off commands to multiple blades from a central
host) but that is irrelevant to my question. The problem I am having
however is that when using the runcmd the   content of variables
contained within command string I pass to it for execution is not being
expanded.

For example if I use:

runcmd <flags> "<complete literal command>"

everything works ok.

But if the following:

runcmd <flags> "<literal command> ${variable} <literal command>"

The runcmd does not work.

What is the best method to expand a variable's content prior to the
execution of runcmd?

 
 
 

expand variable content within a command

Post by Bruce Barnet » Fri, 23 Jun 2006 19:54:27



> But if the following:

> runcmd <flags> "<literal command> ${variable} <literal command>"

> The runcmd does not work.

You don't really explain why it doesn't work.
runcmd <flags> "echo <literal command> ${variable} <literal command>"

might tell us more.

Off hand, I suspect the $variable is being evaluated BEFORE being
passed to the remote server. If this is not what you want to do, then try

runcmd <flags> "<literal command> \${variable} <literal command>"
or perhaps
runcmd <flags> "<literal command> '${variable}' <literal command>"

Test it using echo:

runcmd <flags> "echo <literal command> \${variable} <literal command>"
runcmd <flags> "echo <literal command> '${variable}' <literal command>"

--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.

 
 
 

expand variable content within a command

Post by Dundonal » Fri, 23 Jun 2006 20:01:45




> > But if the following:

> > runcmd <flags> "<literal command> ${variable} <literal command>"

> > The runcmd does not work.

> You don't really explain why it doesn't work.
> runcmd <flags> "echo <literal command> ${variable} <literal command>"

> might tell us more.

> Off hand, I suspect the $variable is being evaluated BEFORE being
> passed to the remote server. If this is not what you want to do, then try

I don't think this is happening, in fact I want the variables to be
evaluated (i.e. content expanded) before the runcmd is executed.  So I
want

runcmd <flags> "<literal command> ${variable} <literal command>"

to turn in to

runcmd <flags> "<literal commands>"

and then the runcmd to be executed.

<snip>

Thanks for your help so far.

 
 
 

expand variable content within a command

Post by Bill Marcu » Fri, 23 Jun 2006 20:10:18


On 22 Jun 2006 03:27:55 -0700, Dundonald

> I'm actually using a command within bladelogic called runcmd
> (simultaneously fires off commands to multiple blades from a central
> host) but that is irrelevant to my question. The problem I am having
> however is that when using the runcmd the   content of variables
> contained within command string I pass to it for execution is not being
> expanded.

> For example if I use:

> runcmd <flags> "<complete literal command>"

> everything works ok.

> But if the following:

> runcmd <flags> "<literal command> ${variable} <literal command>"

> The runcmd does not work.

> What is the best method to expand a variable's content prior to the
> execution of runcmd?

runcmd <flags> "sh -c \"<literal command> ${variable} <literal command>\""

--
Getting there is only half as far as getting there and back.

 
 
 

expand variable content within a command

Post by Dundonal » Fri, 23 Jun 2006 21:17:09



> On 22 Jun 2006 03:27:55 -0700, Dundonald

> > I'm actually using a command within bladelogic called runcmd
> > (simultaneously fires off commands to multiple blades from a central
> > host) but that is irrelevant to my question. The problem I am having
> > however is that when using the runcmd the   content of variables
> > contained within command string I pass to it for execution is not being
> > expanded.

> > For example if I use:

> > runcmd <flags> "<complete literal command>"

> > everything works ok.

> > But if the following:

> > runcmd <flags> "<literal command> ${variable} <literal command>"

> > The runcmd does not work.

> > What is the best method to expand a variable's content prior to the
> > execution of runcmd?

> runcmd <flags> "sh -c \"<literal command> ${variable} <literal command>\""

Thanks for your suggestion Bill. Unfortunately it doesn't work.  To try
and think it through, what order does the active shell (running the
script) process the command? i.e. would it first of all open up the sh
-c command with the supplied string, expand any variable content, then
supply the result to my runcmd?
 
 
 

expand variable content within a command

Post by Chris F.A. Johnso » Fri, 23 Jun 2006 21:36:23





>> > But if the following:

>> > runcmd <flags> "<literal command> ${variable} <literal command>"

>> > The runcmd does not work.

>> You don't really explain why it doesn't work.
>> runcmd <flags> "echo <literal command> ${variable} <literal command>"

>> might tell us more.

>> Off hand, I suspect the $variable is being evaluated BEFORE being
>> passed to the remote server. If this is not what you want to do, then try

> I don't think this is happening, in fact I want the variables to be
> evaluated (i.e. content expanded) before the runcmd is executed.  So I
> want

> runcmd <flags> "<literal command> ${variable} <literal command>"

> to turn in to

> runcmd <flags> "<literal commands>"

> and then the runcmd to be executed.

    That is exactly what is happening. If that's not what you want,
    you need to explain what you do want.

    What is wrong with the expanded variable?

    Is the result supposed to be quoted? For example, does this help:

runcmd <flags> "<literal command> \"${variable}\" <literal command>"

    Try this:

runcmd <flags> "printf "%s\n" "<literal command> \"${variable}\" <literal command>"

    It should print a list of all the argument, one to a line. If the
    result is not what you want, explain why.

--
   Chris F.A. Johnson, author              <http://cfaj.freeshell.org>
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   ===== My code in this post, if any, assumes the POSIX locale
   ===== and is released under the GNU General Public Licence

 
 
 

expand variable content within a command

Post by Dundonal » Fri, 23 Jun 2006 21:52:29


Chris, thanks for your post, I'm trying to absorb as much learning as
possible as I learn how unix shells work.

Quote:>     Try this:

> runcmd <flags> "printf "%s\n" "<literal command> \"${variable}\" <literal command>"

>     It should print a list of all the argument, one to a line. If the
>     result is not what you want, explain why.

I'll give it a try and post back.
 
 
 

expand variable content within a command

Post by Stephane Chazela » Fri, 23 Jun 2006 22:25:21



Quote:> I'm actually using a command within bladelogic called runcmd
> (simultaneously fires off commands to multiple blades from a central
> host) but that is irrelevant to my question. The problem I am having
> however is that when using the runcmd the   content of variables
> contained within command string I pass to it for execution is not being
> expanded.

> For example if I use:

> runcmd <flags> "<complete literal command>"

> everything works ok.

> But if the following:

> runcmd <flags> "<literal command> ${variable} <literal command>"

[...]

Assuming it's a sh type shell that interprets this, try:

set -x
runcmd <flags> "set -x; <literal command> $var..."

--
Stephane

 
 
 

expand variable content within a command

Post by Bruce Barnet » Sat, 24 Jun 2006 10:44:06



>> > runcmd <flags> "<literal command> ${variable} <literal command>"

>> > The runcmd does not work.
> Thanks for your suggestion Bill. Unfortunately it doesn't work.  To try
> and think it through, what order does the active shell (running the
> script) process the command?

Put an "echo" as the first character of the line, and you will see
what arguments "runcmd" gets.

example:
$ variable=myvalue
$ echo runcmd -flags "<literal command> ${variable} <literal command>"
runcmd -flags <literal command> myvalue <literal command>

the variable gets evaluated because it is between "....." quotes.
runcmd never sees the variable - only the value

what shell and what OS are you using?

--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.

 
 
 

1. Expanding variables within variables

Hello all,
    I am working on writing some scripts for a Linux project, and I have hit
a minor snag.  What I need to know is how to substitute a variable within a
variable.  In other words, if I have a variable `foo_list` that contains a
list of devices (deva, devb, decv, etc), and each of these devies has
certain properties (deva_SIZE, deva_SOMETHING, deva_SOMETHINGELSE).  What
I'd like to do is something like:

for i in $foo_list; do
    /bin/someapp $i size $i_SIZE something $i_SOMETHING somethingelse
$i_SOMETHINGELSE
done

    What this should do is expand $i to be deva, devb, or devc, then tack
the _PROPERTY onto the end, and then expand this variable (deva_SOMETHING).
Am I being to obscure?  Is there a way to do such a thing?  Any help is
GREATLY appreciated!

---
Thomas Cameron

2. IDE hard disk crashed. Trying to run Norton Diskedit to repair?

3. Expanding $variable within $difvariable

4. openwin fails in SLS1.03

5. Expand variables within a string

6. what causes telnet/ssh connections to time out on the client/linux side?

7. Expanding a shell variable that may contain shell variables.

8. ...

9. Setting variables in a command line within Make????

10. Execute unix command within script and store in Variable

11. Variables within commands

12. Setting variables in a command line within Make????

13. shell scripting: echoing contents of contents of variable