Created
April 9, 2016 02:59
-
-
Save johnbuhay/ca7306d78d846ad16b22a273611e2dab to your computer and use it in GitHub Desktop.
Useful bash scripts
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 | |
# This script should be sourced | |
case "$LOG_LEVEL" in | |
debug) | |
set -x | |
LOG_DEBUG="DEBUG" | |
LOG_VERBOSE="VERBOSE" | |
LOG_MESSAGE="MESSAGE" | |
;; | |
verbose) | |
LOG_VERBOSE="VERBOSE" | |
LOG_MESSAGE="MESSAGE" | |
;; | |
quiet) | |
LOG_QUIET="QUIET" | |
;; | |
*) | |
echo "LOG_LEVEL not set to 'verbose', 'debug', 'quiet', assuming 'message' level verbosity" | |
LOG_MESSAGE="MESSAGE" | |
;; | |
esac | |
log() { | |
LEVEL=$1 | |
MESSAGE=$2 | |
DATE=`/bin/date` | |
case "$LEVEL" in | |
DEBUG) | |
if [ -n "$LOG_DEBUG" ] | |
then | |
echo "$DATE DEBUG: $MESSAGE" | |
fi | |
;; | |
INFO) | |
if [ -n "$LOG_VERBOSE" ] | |
then | |
echo "$DATE INFO: $MESSAGE" | |
fi | |
;; | |
MESSAGE) | |
if [ -n "$LOG_MESSAGE" ] | |
then | |
echo "$DATE MESSAGE: $MESSAGE" | |
fi | |
;; | |
ERROR) | |
echo "$DATE ERROR: $MESSAGE" >&2 | |
;; | |
esac | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment