Skip to content

Instantly share code, notes, and snippets.

@ssledz
Last active December 4, 2017 17:00
Show Gist options
  • Save ssledz/8b3268b1e1097c74cd445f34f25615d2 to your computer and use it in GitHub Desktop.
Save ssledz/8b3268b1e1097c74cd445f34f25615d2 to your computer and use it in GitHub Desktop.
bash lib : system out
#!/bin/bash
[[ -z $DBG ]] && DBG=0
now() {
date +"%Y-%m-%d %H:%M:%S"
}
eecho() {
local label=$1
shift
local msg=
if [[ $# == 0 ]]; then
msg=$(cat -)
else
msg="$*"
fi
echo -e "$label : "$msg
}
dbg() {
[[ $DBG == 1 ]] && (1>&2 eecho "[DEBUG]" $*)
}
info() {
eecho "$(now)" $*
}
warn() {
eecho "[WARN]" $*
}
err() {
(1>&2 eecho "[ERROR]" $*)
}
@ssledz
Copy link
Author

ssledz commented May 25, 2017

Example of usage:

echo foo bar | info 
2017-05-25 14:33:26 : foo bar
info foo bar
2017-05-25 14:33:31 : foo bar
warn foo bar
[WARN] : foo bar
echo 'foo bar' | warn        
[WARN] : foo bar
dbg foo bar
DBG=1 dbg foo bar
[DEBUG] : foo bar
DBG=0 dbg foo bar
echo foo bar | DBG=1 dbg
[DEBUG] : foo bar

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