Skip to content

Instantly share code, notes, and snippets.

@d33pcode
Last active November 7, 2017 14:07
Show Gist options
  • Save d33pcode/d6db515f4dcc3ed23be1ad9c4c7e53b9 to your computer and use it in GitHub Desktop.
Save d33pcode/d6db515f4dcc3ed23be1ad9c4c7e53b9 to your computer and use it in GitHub Desktop.
colorlog
logfile="$(pwd)/amazing.log"
###################
# ANSI escape codes
###################
RED='\033[0;31m'
LRED='\033[1;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
NC='\033[0m' # nocolor
#####################################
# Logs a string to stdout and logfile
#####################################
# Arguments:
# $1: string to log
# $2: optional color
#####################################
function log() {
echo -ne "${2:-${NC}}" |& tee -a ${logfile}
echo -ne "$1" |& tee -a ${logfile}
echo -e "${NC}" |& tee -a ${logfile}
}
######################################
# Logs a command to stdout and logfile
######################################
# Arguments:
# $1: command to execute and log
# $2: optional color
#####################################
function log_exec() {
echo -ne "${2:-${NC}}" |& tee -a ${logfile}
eval $1 |& tee -a ${logfile}
echo -e "${NC}" |& tee -a ${logfile}
}
log "Test with folder compression" ${GREEN}
log_exec "mkdir -vp /tmp/test/folder/to/compress" ${CYAN}
log_exec "tar cvfJ /tmp/compressed.tar.xz /tmp/test/folder/to/compress" ${RED}
log_exec "rm -rfv /tmp/test /tmp/compressed.tar.xz" ${LRED}
log "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment