Last active
September 22, 2017 22:05
-
-
Save mangosmoothie/835366e8545297a00b2536e47bd63050 to your computer and use it in GitHub Desktop.
function to log & print output of bash commands while preserving exit codes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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