Skip to content

Instantly share code, notes, and snippets.

@pysysops
Last active November 20, 2019 09:33
Show Gist options
  • Save pysysops/000b6ea2711b8c700b4cb08c4072d4f7 to your computer and use it in GitHub Desktop.
Save pysysops/000b6ea2711b8c700b4cb08c4072d4f7 to your computer and use it in GitHub Desktop.
Template for a bash script that has simple logging and color / formatting presets
#!/usr/bin/env bash
# Surrounding in brackets ensures that the whole script gets loaded to memory
# before running which allows us to update the script from the script without
# issue.
{
set -eu -o pipefail
# Let's make some pretty stuff
COLOR_RESET="$(tput sgr0)"
COLOR_BLACK="$(tput setaf 0)"
COLOR_RED="$(tput setaf 1)"
COLOR_GREEN="$(tput setaf 2)"
COLOR_YELLOW="$(tput setaf 3)"
COLOR_BLUE="$(tput setaf 4)"
COLOR_MAGENTA="$(tput setaf 5)"
COLOR_CYAN="$(tput setaf 6)"
COLOR_WHITE="$(tput setaf 7)"
TEXT_BOLD="$(tput bold)"
SYMBOL_DEBUG=""
SYMBOL_INFO=""
SYMBOL_WARN=""
SYMBOL_TICK=""
SYMBOL_CROSS=""
log_debug() {
if [ ${DEBUG:-0} -eq 1 ]; then
echo "${COLOR_RESET}${SYMBOL_DEBUG} $@ ${COLOR_RESET}"
fi
}
log_info() {
echo "${COLOR_CYAN}${SYMBOL_INFO} $@ ${COLOR_RESET}"
}
log_ok() {
echo "${COLOR_GREEN}${SYMBOL_TICK} $@ ${COLOR_RESET}"
}
log_warn() {
echo "${COLOR_YELLOW}${SYMBOL_WARN} $@ ${COLOR_RESET}"
}
log_err() {
>&2 echo "${TEXT_BOLD}${COLOR_RED}${SYMBOL_CROSS} $@ ${COLOR_RESET}"
}
# Your script here...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment