Thread View: comp.unix.shell
3 messages
3 total messages
Started by Chris Shenton
Mon, 10 Dec 1990 21:03
Delayed evaluation of csh variables?
Author: Chris Shenton
Date: Mon, 10 Dec 1990 21:03
Date: Mon, 10 Dec 1990 21:03
19 lines
1063 bytes
1063 bytes
I'm trying to do a script where I set up a variable to contain a template, then later instantiate one of the variables; the whole thing then is passed to a command. Something like this, conceptually: #!/bin/csh set COMMAND = "command" set OPTION_TEMPLATE = "-Z '$File'" # Want *literal* $File # -- to be evaluated *later* -- # but csh evaluates it now and dies foreach File ( $* ) # Instantiate in $OPTION_TEMPLATE ?? $COMMAND $OPTION_TEMPLATE # doesn't work end Is there a way to do this? Thanks in advance. -- chris@asylum.gsfc.nasa.gov, ...!uunet!asylum.gsfc.nasa.gov!chris, PITCH::CHRIS
Re: Delayed evaluation of csh variables?
Author: Tom Christiansen
Date: Tue, 11 Dec 1990 00:24
Date: Tue, 11 Dec 1990 00:24
38 lines
1944 bytes
1944 bytes
In article <CHRIS.90Dec10160308@asylum.gsfc.nasa.gov> chris@asylum.gsfc.nasa.gov (Chris Shenton) writes:
:I'm trying to do a script where I set up a variable to contain a template,
:then later instantiate one of the variables; the whole thing then is passed
:to a command. Something like this, conceptually:
:
:#!/bin/csh
You forgot the -f there. That'll slow you down a lot and
could have unforeseen side-effects.
:set COMMAND = "command"
:set OPTION_TEMPLATE = "-Z '$File'" # Want *literal* $File
: # -- to be evaluated *later* --
: # but csh evaluates it now and dies
:foreach File ( $* ) # Instantiate in $OPTION_TEMPLATE ??
: $COMMAND $OPTION_TEMPLATE # doesn't work
:end
:Is there a way to do this?
Sure, you could use an eval (we seem to get kind of question one a lot).
One of your problems is using the csh at all. It's a real pain. I've
heard Bill Joy will tell you he didn't understand expression evaluation
when he wrong it. It does show. You could do this:
set OPTION_TEMPLATE = '-Z $File' # Want *literal* $File
and then this:
eval $COMMAND $OPTION_TEMPLATE
but please trust me when I tell you that you do not want to be
writing scripts in csh. Honest.
--tom
--
Tom Christiansen tchrist@convex.com convex!tchrist
"With a kernel dive, all things are possible, but it sure makes it hard
to look at yourself in the mirror the next morning." -me
Re: Delayed evaluation of csh variables?
Author: Chris Shenton
Date: Tue, 11 Dec 1990 17:37
Date: Tue, 11 Dec 1990 17:37
27 lines
1453 bytes
1453 bytes
Thanks to everyone who responded (mail and posts) on my query. This is a ``best-of'' from the suggestions. 1. The quoting can be simplified by nixing the " and using ' instead. 2. Eval -- this is the key -- is needed to instantiate the variable in the string. Here's the simplest example thing I've come up with: #!/bin/csh -vxf # show whazzup; ignore .cshrc set COMMAND = 'ls -l $File' # simple single quotes do the trick foreach File ( $* ) # do all command line arguments eval $COMMAND # eval to instantiate $File end tchrist@convex.com insists that I shouldn't be doing any programming in csh. Probably true, but I'm too lazy to get intimate with sh for such short stuff. (I'd rather learn bash!) Again, many thanks to everyone. Ciao! -- chris@asylum.gsfc.nasa.gov, ...!uunet!asylum.gsfc.nasa.gov!chris, PITCH::CHRIS
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads