[clug] Next in my series of "how to do things in /bin/sh that you probably shouldn't".
Kevin Pulo
kev at pulo.com.au
Wed Aug 19 20:13:07 MDT 2009
Wow, this is wierd alright.
This doesn't work (hangs):
exec > >(tee ${0##*/}.log &) 2>&1
This sometimes works, sometimes doesn't. There's clearly a race with
the startup of tee. And, as you say, sometimes the final line(s) of
output come after the script has finished.
logfile=>(tee ${0##*/}.log)
exec > "$logfile" 2>&1
Adding "wait" at the end doesn't help, not even if "set -m" is used at
the start. Adding 1 sec sleeps before/after the exec doesn't fix it
either.
There's another way, but it's "cheating" and will cost you an extra
process. And you have to pick an environment variable that won't
clash (unless you want to muck around using eval to set an envvar
based on $$ (in the parent) and $PPID (in the child)). I suppose it's
somewhat more reliable than using pipe-based redirections with exec,
since it uses processes more straightforwardly.
#!/bin/bash
if [ "x$redir" = "x" ]; then
redir=y "$0" "$@" 2>&1 | tee ${0##*/}.log
exit $?
else
unset redir
fi
...
Kev
On Thu, Aug 20, 2009 at 11:21:31AM +1000, Kevin Pulo wrote:
> Now that I bother actually trying it, it doesn't work at all for me.
> It just hangs on the exec line waiting for the tee to finish. :( So
> obviously it's not quite there yet...
>
> Kev
>
>
> On Thu, Aug 20, 2009 at 11:09:28AM +1000, Andrew Janke wrote:
>
> > > Not that I've tried it, but using process substitution to get bash to
> > > automatically do the pipe magic might work?
> > >
> > > exec > >(tee logfile.txt) 2>&1
> >
> > Hrm, this certainly works as a bash script but the prompt is ahead of
> > itself after running it? I suspect as the script finishes and the
> > prompt is returned before the tee is. I'd have to add a wait on the
> > tee (presuming I can get its PID) at the end I would guess.
> >
> > Still a whole lot shorter!
> >
> >
> > a
> >
> >
> >
>
> Kev.
>
Kev.
--
.----------------------------------------------------------------------.
| Kevin Pulo Quidquid latine dictum sit, altum viditur. |
| kev at pulo.com.au _ll l_ng__g_e_ _r_ hi__ly p__d_ct__le. |
| http://www.kev.pulo.com.au/ God casts the die, not the dice. |
`--------------- Linux: The choice of a GNU generation. ---------------'
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.samba.org/pipermail/linux/attachments/20090820/98009d90/attachment-0001.pgp>
More information about the linux
mailing list