Skip to content

Instantly share code, notes, and snippets.

@mangosmoothie
Last active September 22, 2017 22:05
Show Gist options
  • Save mangosmoothie/835366e8545297a00b2536e47bd63050 to your computer and use it in GitHub Desktop.
Save mangosmoothie/835366e8545297a00b2536e47bd63050 to your computer and use it in GitHub Desktop.
function to log & print output of bash commands while preserving exit codes
#!/bin/bash
# function to wrap a command with logging that also returns
# the status code of the command
function run() {
echo "running $@" |& tee -a out.log
eval $@ > >(tee -a out.log) 2> >(tee -a out.log)
return $?
}
run "echo this will log" && \
run "sh -c 'exit 1'" && \
run "echo last command non zero exit code so this will not log'";
run "echo doing some more stuff";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment