Created
May 21, 2013 10:07
-
-
Save habibutsu/5618756 to your computer and use it in GitHub Desktop.
Execute command in bash with copy stderr into variable and stdout into stdout of script
This file contains hidden or 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
function exec_cmd { | |
exec 3>&1 | |
error=$( { $@ 1>&3; } 2>&1 ) | |
exec 3>&- | |
if [ "${error}" ]; then | |
echo "[ERROR] ${error}" | |
exit 1 | |
fi | |
} | |
# Example usage | |
exec_cmd ls -la |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment