Files
CodeSnippets/bash/internal_output_tee.bash

16 lines
962 B
Bash

#!/bin/bash
# This will tee both stout and sterr to terminal and logfile for the entirity of the script.
# Useful if you cannot / don't want to wrap the script execution in a redirect at call time.
# Stops you having to put '...| tee $LOGFILE 2>&1' on every other line of your script.
LOGFILE="/my/log/file.log"
exec > >(tee "$LOGFILE") 2>&1
# If you have tput colouring, etc, in your scripts for pretty terminal output, then the following
# version of the above will strip the control codes so they don't pollute the log file. Not required
# if you'll always be looking at the log file with a smart enough viewer, but this isn't always the
# case, or you might want additional machine parsing of logs.
exec > >(tee >(sed -r "s/\x1b\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | sed "s/\x0f//g" > $BUILDLOG)) 2>&1
# First sed removes the setaf colour codes, second sed is required because despite all other attempts
# there remains a leftover code from sgr0 resets.