Am I crazy? I thought this could be made to work...

Am I crazy? I thought this could be made to work...

Post by Lo » Wed, 03 Mar 2004 13:33:55



Hi All,

In the short example below, I've tried various quotes and
escapes with no luck.  Is this impossible, or am I
just tired???  I want to put a command inside a variable,
but the command references a variable thaty is not
yet defined.  Boy am I rusty on this stuff!

Any info is appreciated...
-Lou V.

#!/bin/ksh

##########################################################
# Is it possible to store a command in a variable, with
# an embedded variable in that command and assign a value
# to the embedded variable LATER, and then execute the
# command?
#
# I want the 'for loop' to literally translate to:
#
#      for item in `grep cookies /some/file | cut -d: -f1`
#
##########################################################

command="grep $string /some/file | cut -d: -f1"

# Set $string value  a bit later...

string=cookies

for item in `$command`
do
    echo $item is the first field in a line that has cookies.
done

 
 
 

Am I crazy? I thought this could be made to work...

Post by Barry Margoli » Wed, 03 Mar 2004 14:14:14




> Hi All,

> In the short example below, I've tried various quotes and
> escapes with no luck.  Is this impossible, or am I
> just tired???  I want to put a command inside a variable,
> but the command references a variable thaty is not
> yet defined.  Boy am I rusty on this stuff!

> Any info is appreciated...

Since you put the string in double quotes, the variable is expanded at
the time of the assignment.

If you want it to be expanded later, you must use single quotes in the
assignment, and use the "eval" command when you execute it.

Quote:

> #!/bin/ksh

> ##########################################################
> # Is it possible to store a command in a variable, with
> # an embedded variable in that command and assign a value
> # to the embedded variable LATER, and then execute the
> # command?
> #
> # I want the 'for loop' to literally translate to:
> #
> #      for item in `grep cookies /some/file | cut -d: -f1`
> #
> ##########################################################

> command="grep $string /some/file | cut -d: -f1"

> # Set $string value  a bit later...

> string=cookies

> for item in `$command`
> do
>     echo $item is the first field in a line that has cookies.
> done

--

Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

 
 
 

Am I crazy? I thought this could be made to work...

Post by Lo » Thu, 04 Mar 2004 03:24:37


Thanks, Barry.  I would have gotten the quoting right eventually if I
had thought about eval.  I was presuming the shell could handle this
directly.

Cheers,
Lou


> Since you put the string in double quotes, the variable is expanded at
> the time of the assignment.

> If you want it to be expanded later, you must use single quotes in the
> assignment, and use the "eval" command when you execute it.

 
 
 

1. This clone thing...am I stupid, or am I right?


says...

I admire your passion, Chris..

The only issues which have kept myself from looking seriously at Linux
would be:

* It's Unix and the learning curve to start getting productive strikes me
as probably very steep

* It doesn't support plug-and-play.  If your hardware doesn't have Linux
drivers, it don't play..

* Not nearly the amount of developers out there doing stuff for Linux as
there are for Wintel.  Can I get MS Office for Linux?  Can I get
Photoshop for Linux?  Can I get QuarkXpress for Linux? etc.. a big turn
off for me.

--
Reuben King
Email: "reuben at texas dot net" (in plain english to foil spam-bots.
grrr!)

2. Telnet does not work...

3. ** Memory Usage Display (gmemusage - linux?)

4. I am with the following error, when i am running lilo...

5. small file but: Filesize limit exceeded

6. Am I touchy? Or am I right?

7. Win 95 PPP problem with Solaris

8. Am I seeing IPv5, or am I hallucinating?

9. I am buying an Ultra 5 but am lost in part numbers ....

10. I am in text mode, what browser am I running....

11. I am si**y am I?

12. am I crazy?