Skip to content

Instantly share code, notes, and snippets.

@frntn
Last active August 21, 2017 09:31
Show Gist options
  • Save frntn/d5b8528858ce2abb64942512c020678b to your computer and use it in GitHub Desktop.
Save frntn/d5b8528858ce2abb64942512c020678b to your computer and use it in GitHub Desktop.
Colored File Descriptor
#!/bin/bash
# just put this at the beginning of your scripts
trap "{ sleep 1; }" EXIT
red="" ; [ -t 1 ] && [ -t 2 ] && red="$(tput setaf 1)"
rst="" ; [ -t 1 ] && [ -t 2 ] && rst="$(tput sgr0)"
logdir="/tmp"
exec 3> "$logdir/raw.log"
exec 2> >( awk '{print strftime("%Y%m%dT%H:%M:%S%z")" - STDERR - "$0 ; fflush(stdout)}' | tee -a $logdir/${0%.*}.err > >( awk '{print "'$red'"$0"'$rst'" ; fflush(stdout)}' ))
exec 1> >( awk '{print strftime("%Y%m%dT%H:%M:%S%z")" - STDOUT - "$0 ; fflush(stdout)}' | tee -a $logdir/${0%.*}.out)
# example
echo stdout
echo stderr >&2
echo rawout >&3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment