Last active
December 19, 2015 07:09
-
-
Save Darkhogg/5917104 to your computer and use it in GitHub Desktop.
A shell function that executes a command and tells if it succeeded or failed with customizable output.
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
#$ okko [opts] message command [args]* | |
# | |
# This shell function provides a way to run a command and present its result | |
# in a user-friendly way. | |
# | |
# The provided message is initially printed to the screen, then the command is | |
# run with the given arguments. After command completion, 'ok' is printed if | |
# the command exited with a tatus of 0, else 'err(N)' is printed, where N is | |
# the error code. | |
# | |
# Everything output by this command can be overriden using environment | |
# variables as follows: | |
# OKKO_MESSAGE The message format as a printf string. The user-specified | |
# mesage is passed as the first argument, and should be | |
# specified as %b to allow escapes. | |
# OKKO_SUCCESS The printf format for a successful command. No arguments are | |
# passed to printf. | |
# OKKO_FAILURE The printf format for a failed command. The error code is | |
# passed as the first argument. | |
# | |
# At any time, set these variables to an empty string to return to the default | |
# formats. Be aware that everything is output using printf, which makes you | |
# explicitly write the final newline character. | |
# | |
# The exit status of this function is set to the exit status of the command, so | |
# you may give more useful information of what failed, if available. | |
okko() { | |
[ -z "$OKKO_MESSAGE" ] && OKKO_MESSAGE="%b " | |
[ -z "$OKKO_SUCCESS" ] && OKKO_SUCCESS="ok\n" | |
[ -z "$OKKO_FAILURE" ] && OKKO_FAILURE="err(%d)\n" | |
printf "$OKKO_MESSAGE" "$1" | |
shift | |
"$@"; local E="$?" | |
[ "$E" -eq "0" ] \ | |
&& printf "$OKKO_SUCCESS" \ | |
|| printf "$OKKO_FAILURE" "$E" | |
return "$E" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment