Skip to content

Instantly share code, notes, and snippets.

@paulc
Last active April 8, 2021 22:15
Show Gist options
  • Save paulc/6334431 to your computer and use it in GitHub Desktop.
Save paulc/6334431 to your computer and use it in GitHub Desktop.
Simple shell wrapper to log execution & return status of command and (optionally) colourise output.
trap 'rm -f /tmp/err.* /tmp/out.*' EXIT
_NORMAL=$(printf "\033[0m")
_RED=$(printf "\033[0;31m")
_YELLOW=$(printf "\033[0;33m")
_CYAN=$(printf "\033[0;36m")
export _CLOG=${_CLOG:-log-$(date +%Y%m%d%H%M%S)}
_c() {
_cmd=$(printf '"%s" ' "$@")
printf "%s [%s] %-40s\n" "$(date '+%b %d %T')" $name "CMD: $_cmd"
eval "$_cmd" 2>&1 | sed -e 's/^/ | /'
_status=$?
printf " "
[ $_status -eq 0 ] && printf "[OK]\n" || printf "[ERROR]\n"
return $_status
}
_cc() {
_cmd=$(printf '"%s" ' "$@")
_out=$(mktemp -t out)
_err=$(mktemp -t err)
printf "${_RED}"
printf "%s [%s] %-40s\n" "$(date '+%b %d %T')" $name "CMD: $_cmd"
printf "${_NORMAL}"
eval "$_cmd" >${_out} 2>${_err}
_status=$?
printf "${_CYAN}"
sed -e 's/^/ | /' <${_out}
printf "${_YELLOW}"
sed -e 's/^/ ! /' <${_err}
printf "${_RED} "
[ $_status -eq 0 ] && printf "[OK]\n" || printf "[ERROR]\n"
printf "${_NORMAL}"
rm -f ${_out} ${_err}
return $_status
}
_cl() {
_cmd=$(printf '"%s" ' "$@")
_out=$(mktemp -t out)
_err=$(mktemp -t err)
printf "${_RED}"
printf "%s [%s] %-40s\n" "$(date '+%b %d %T')" $name "CMD: $_cmd"
printf "${_NORMAL}"
eval "$_cmd" >${_out} 2>${_err}
_status=$?
printf "${_CYAN}"
sed -e 's/^/ | /' <${_out}
printf "${_YELLOW}"
sed -e 's/^/ ! /' <${_err}
printf "${_RED} "
[ $_status -eq 0 ] && printf "[OK]\n" || printf "[ERROR]\n"
printf "${_NORMAL}"
printf "%s [%s] %-40s\n" "$(date '+%b %d %T')" $name "CMD: $_cmd" >> ${_CLOG}
sed -e 's/^/ | /' <${_out} >> ${_CLOG}
sed -e 's/^/ ! /' <${_err} >> ${_CLOG}
[ $_status -eq 0 ] && printf "[OK]\n\n" >> ${_CLOG} || printf "[ERROR]\n\n" >> ${_CLOG}
rm -f ${_out} ${_err}
return $_status
}
[ ! -z "$@" ] && _cc "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment