Subject: Re: Bourne shell oddity
To: None <martin.ammermueller@bnro.de>
From: Havard Eidnes <he@uninett.no>
List: current-users
Date: 06/22/2005 13:41:04
> Havard Eidnes wrote on 2005-06-21|16:35:
> > -------------------- /tmp/x:
> > #! /bin/sh
> >
> > echo x: before
> > ( sh -c "sleep 60" ) > /dev/null 2>&1 </dev/null &
> > echo x: after
> > exit 0
> > -------------------- /tmp/y:
> > #! /bin/sh
> >
> > echo y: before
> >
> > l=3D`/tmp/x`
> >
> > echo y: after
> > --------------------
> >
> > If you run "sh /tmp/y", the "y: after" output will not appear
> > until 60 seconds has passed, i.e. the innermost background job
> > finishes, even though that job has no chance of affecting the
> > result due to the redirection.
>
> I don't think that's a bug. According to the sh(1) manpage, the
> backquotes will be replaced by the output of the enclosed command.
> But how can you get the complete output of the enclosed command
> before it has finished?

Please notice that the subshell doing the "sleep 60" is a
background job, so the enclosing script should finish right away,
as it does when run directly from the command line.  Also note
that the subshell runs with stdout and stderr redirected.  That's
two reasons the output of the backquoted command will not be
affected.

Also note that if /tmp/x is changed to a /bin/ksh or /bin/bash
script, the "hang" does not happen, as is also the case if the
sleep is not done in a subshell, i.e. the parenthesis are
removed.

Regards,

- H=E5vard