[clug] Next in my series of "how to do things in /bin/sh that you probably shouldn't".
David Deaves
David.Deaves at dd.id.au
Thu Aug 20 06:32:36 MDT 2009
> > 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!
>
Those bash process substitutions can do some pretty cool thing, but they
are a bit strange. "$!" will get the PID of the process, however you cannot
wait on it with or without the PID argument. Recently I worked around it with:
while kill -0 $sub_proc_pid
do
sleep 1
done
and had to pass status through a file.
But a simpler solution to the above problem is :
exec > >(tee logfile.txt) 2>&1
do stuff
rc=$?
exec >/dev/null 2>&1 # Close the inputs to the tee
sleep 2 # Yield and give tee some time to wrap up
exit $rc
Dave !
More information about the linux
mailing list