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...
Ooh… ace! I never got the hang of that redirecting other streams thing.
Shit. Your computer’s gone wrong
leave a reply
You must be logged in to post a comment.