BASH Logging Function
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
date
command is stored in$today
- All arguments to the function (
$@
) are executed and stdout and stderr are redirected to files.>
redirectsstdout
and2>
redirectsstderr
>( )
is aprocess substitution
construction. The>
in front means that the input passed to the subprocess will be treaded asstdin
.- That
stdin
is redirected to a file bytee
.