Friday, January 11, 2008

Using Shell file redirection to create a transcript of all content written out from a script

# #################################################
#
#Redirect to file while keep output writing to the console
# This could be used to create a transcript of your files. #
##################################################
start_transcript(){
declare -x OUTPUT_TRANSCRIPT=/tmp/$TRANSCRIPT_FILE
declare -x OUTPUT_PIPE=/tmp/$PIPE_FILE
if [ ! -e $OUTPUT_PIPE ]; then
mkfifo $OUTPUT_PIPE
fi
if [ -e $OUTPUT_TRANSCRIPT ]; then
rm -vf $OUTPUT_TRANSCRIPT
fi
exec 6>&1 7>&2
tee $OUTPUT_TRANSCRIPT < $OUTPUT_PIPE >&6 &
tpid=$!
exec > $OUTPUT_PIPE 2>&1
}
export -f start_transcript

end_transcript(){
echo "closing output pipe $OUTPUT_PIPE"
exec 1>&6 6>&- 2>&7 7>&-
wait $tpid
rm -vf $OUTPUT_PIPE
}
export -f end_transcript


# Using the transcript functions
declare -x TRANSCRIPT_FILE={$0##/*}.$$.transcript
declare -x PIPE_FILE=$TRANSCRIPT_FILE.pipe
start_transcript
echo "transcript started with - ${TRANSCRIPT_FILE} and pipe ${PIPE_FILE}"

#this is where all your main code will be

# at the end of the file
end_transcript #

No comments: