May 11

If you want your bash script to record a logfile, but also to give you stdout and stderr separately so you can redirect as normal, here’s a starting point:

#!/usr/bin/bash

LOGFILE=teeself.log
# temp file names use computer's nodename and script's own PID
PIPE_OUT=.tmp_out_`uname -n`_$$
PIPE_ERR=.tmp_err_`uname -n`_$$

# create a couple of buffers for stdout and stderr
mkfifo $PIPE_OUT
mkfifo $PIPE_ERR

# tee each stream to the logfile.  By keeping them separate we end up with
# stdout and stderr behaving themselves when we call the script.
rm -v $LOGFILE
tee -a $LOGFILE < $PIPE_OUT &
tee -a $LOGFILE <$PIPE_ERR 1>&2 &

# redirect the rest of output to the two streams
exec 1>$PIPE_OUT
exec 2>$PIPE_ERR

# Here's the (trivial test) script
echo stdout
echo stderr 1>&2

# Tidy up.
rm -v $PIPE_OUT $PIPE_ERR


2 comments so far...

  • stu Said on May 11th, 2007 at 13:06:

    Ooh… ace! I never got the hang of that redirecting other streams thing.

  • lordhutton Said on May 11th, 2007 at 20:37:

    Shit. Your computer’s gone wrong

leave a reply

You must be logged in to post a comment.

 

May 2007
S M T W T F S
« Apr   Jun »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Archives

Meta