[comp.unix.questions] background foreach in csh or for in sh?

gwoho@nntp-server.caltech.edu (g liu) (03/25/91)

how do i do a foreach or for in the background without
writing a script file or explicitly running a new shell
or something complicated like that??
is there a simple way to do it?

gwoho liu.

pfalstad@phoenix.Princeton.EDU (Paul Falstad) (03/25/91)

gwoho@nntp-server.caltech.edu (g liu) wrote:
>how do i do a foreach or for in the background without
>writing a script file or explicitly running a new shell
>or something complicated like that??
>is there a simple way to do it?

sh:

for i in whatever
do
...
done &

(just stick an & after the done)

I tried various ways to do this in csh, including putting the whole thing in
a subshell explicitly and putting \'s at the end of every line, or
embedding control-J's in the line, but it wouldn't work.  There might be
a simple way, but who cares?  Use sh anyway; its behavior is much more
predictable and reliable than csh's, especially for shell scripts.

--
Paul Falstad, pfalstad@phoenix.princeton.edu | 10 PRINT "PRINCETON CS"
[Your blood pressure just went up.]          | 20 GOTO 10
Princeton University would like to apologize to everyone for this article.

phil@ux1.cso.uiuc.edu (Phil Howard KA9WGN) (03/26/91)

gwoho@nntp-server.caltech.edu (g liu) writes:

>how do i do a foreach or for in the background without
>writing a script file or explicitly running a new shell
>or something complicated like that??
>is there a simple way to do it?

>gwoho liu.

Is it that complicated to run a new shell?  I assume you are using csh.
Use the "-f" option to supress ".cshrc" execution for faster startup of
the subshell.

echo 'foreach number ( 0 1 2 3 4 5 6 7 8 9 )\
echo the number is $number\
end' | csh -f &

Note that you MUST use SINGLE quotes here, NOT double quotes.  The reason
is because a double quote will allow the $number variable to be interpreted
in your command level shell, and it is either undefined or won't have what
you want in it.  The backslashes are to put newlines into the data echoed
by the first echo.  When I do these types of things, I FIRST test the echo
data by NOT piping it into the subshell:

echo 'foreach number ( 0 1 2 3 4 5 6 7 8 9 )\
echo the number is $number\
end'

Then when I decide I did it right, I type this:

!! | csh -f &

and it will take off.
-- 
 /***************************************************************************\
/ Phil Howard -- KA9WGN -- phil@ux1.cso.uiuc.edu                              \
\ Lietuva laisva -- Brivu Latviju -- Eesti vabaks                             /
 \***************************************************************************/

weimer@garden.kodak.COM (Gary Weimer (588-0953)) (03/27/91)

In article <1991Mar25.231824.9992@ux1.cso.uiuc.edu>,
phil@ux1.cso.uiuc.edu (Phil Howard KA9WGN) writes:
|> Is it that complicated to run a new shell?  I assume you are using csh.
|> Use the "-f" option to supress ".cshrc" execution for faster startup of
|> the subshell.
|> 
|> echo 'foreach number ( 0 1 2 3 4 5 6 7 8 9 )\
|> echo the number is $number\
|> end' | csh -f &

I use this so often, that I've written an alias for it:

alias loop      '(echo foreach x\(\!:1\);echo \!:2*;echo end)|csh -f &'

To use the alias:

loop '0 1 2 3 4 5 6 7 8 9' 'echo the number is $x'

(Note that the loop variable is always $x)
If you don't use the single quotes, you will have to escape $, *, etc.
I often use this on my server for more complicated things like:

loop '/export/root/*/etc' 'mv $x/printcap $x/printcap.orig; cp
    /etc/printcap $x/printcap'
(should be all one line)

This will install the server's new /etc/printcap file on all it's clients.

weimer@ssd.kodak.com ( Gary Weimer )