BASH Logging Function
Updated Sun 2016-12-25 | 105 words
This is a small BASH logging function to record the output of a command:
function logit {
today=$(date +%Y-%m-%d.%H.%M)
"$@" > >(tee "$1".stdout."$today".log) 2> >(tee "$1".stderr."$today".log >&2)
}
How it works:
- The output of the
datecommand is stored in$today - All arguments to the function (
$@) are executed and stdout and stderr are redirected to files.>redirectsstdoutand2>redirectsstderr>( )is aprocess substitutionconstruction. The>in front means that the input passed to the subprocess will be treaded asstdin.- That
stdinis redirected to a file bytee.