-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
At some point hitherto, Kevin D. Clark hath spake thusly:
> > result=badness # init with failure default
> > spewSomeKindOfOutput | while read input
> > do
> > result=goodness
> > done
> > echo $result
> >
> > What is the output?
>
> In general, the inner part of the loop is run in a sub-shell.
Not exactly... it's more subtle even than that. For example:
result=badness # init with failure default
while read input
do
result=goodness
done
echo $result
What is the result in this case? The shell script outputs "goodness"
instead of "badness" as in the previous example.
Certain kinds of things trigger the shell to use a subshell for loops.
One is, as in the example above, piping the output of a process into
another command. Another is if you redirect the output of the loop
into a file, as in
done >> $ouputfile
In the former case, I believe it does so because the shell which the
script is running in must first fork a process to generate the output,
and fork a second process (a shell) to feed the output of that command
to through a pipe (as in pipe(2)). I believe this is necessary
because the stdout and stdin of the processes which the shell runs are
not the same as those of the shell itself. I'm not quite positive on
that, and hopefully someone will correct me if I'm mistaken. The
latter case would essentially be the same thing; the shell the loop is
running in has a different stdout than the shell script itself, so it
must fork a subshell in order to change where stdout goes for the
subprocess without changing it for the parent.
I think the redirection would look something like this:
s = fork();
if (!s){
fd = open("outputfile", blah);
dup2(fd, 1);
execv("/usr/bin/bash", "/usr/bin/bash", blah blah blah);
}
/* continue processing the rest of the shell script */
In my minor rewrite of Mike's example, this isn't necessary, because
stdout, stdin, and stderr haven't been redirected (nor crammed into a
pipe), but are all the same in the loop as for the rest of the shell
script.
- --
Derek Martin [EMAIL PROTECTED]
- ---------------------------------------------
I prefer mail encrypted with PGP/GPG!
GnuPG Key ID: 0x81CFE75D
Retrieve my public key at http://pgp.mit.edu
Learn more about it at http://www.gnupg.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org
iD8DBQE8xNm+djdlQoHP510RAnrZAJ97gDtySuBILkhU6HEJTwiXLEQlFQCghMYN
h2WnKkdUxE4MFaKl6a7kcgc=
=594z
-----END PGP SIGNATURE-----
*****************************************************************
To unsubscribe from this list, send mail to [EMAIL PROTECTED]
with the text 'unsubscribe gnhlug' in the message body.
*****************************************************************