diff --git a/bash/internal_output_tee.bash b/bash/internal_output_tee.bash index a8cd8cf..6ce8cfa 100644 --- a/bash/internal_output_tee.bash +++ b/bash/internal_output_tee.bash @@ -1,8 +1,15 @@ #!/bin/bash -LOGFILE="/my/log/file.log" -exec > >(tee "$LOGFILE") 2>&1 # 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. -# \ No newline at end of file +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.