Skip to content

Instantly share code, notes, and snippets.

@tonybaines
Created December 23, 2015 08:25
Show Gist options
  • Save tonybaines/0ad3040a9e3a55ef5e6b to your computer and use it in GitHub Desktop.
Save tonybaines/0ad3040a9e3a55ef5e6b to your computer and use it in GitHub Desktop.
Useful bash scripting functions
# foundation function for the actual log-functions to build on
function _log {
echo "[$(date +%Y%m%d-%T)] $1"
}
# functions for the different log levels
function log_debug {
# debug messages need to be enabled through an environment varaible
if [ $DEBUG ]
then
_log "DEBUG: $1"
fi
}
function log_info {
_log "INFO: $1"
}
function log_warn {
_log "WARN: $1"
}
function log_error {
_log "ERROR $1"
}
# For when the process needs to exit with an error message
function die() {
log_error "$1"
exit 1
}
@tonybaines
Copy link
Author

Example use

log_info "Checking pre-requisites"
which svn >/dev/null || die "The 'svn' command is not available on the PATH"

To show debug messages

DEBUG=1 my_script.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment