Last active
February 13, 2017 13:56
-
-
Save mmichaelis/0f6397854d0b679753f7 to your computer and use it in GitHub Desktop.
Template with some convenience settings for 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
#!/usr/bin/env bash | |
set -o nounset | |
set -o pipefail | |
set -o errexit | |
### Uncomment to enable debugging | |
#set -o verbose | |
#set -o xtrace | |
declare -r BOLD="$(tput bold)" | |
declare -r DIM="$(tput dim)" | |
declare -r NORMAL="$(tput sgr0)" | |
declare -r SMUL="$(tput smul)" | |
declare -r RMUL="$(tput rmul)" | |
declare -r LRED="${BOLD}$(tput setaf 1)" | |
function realpath() { | |
$(type -p greadlink readlink | head -1) -f "${@}" | |
} | |
declare -r MY_CMD="$(realpath "${0}")" | |
declare -r MY_DIR="$(dirname "${MY_CMD}")" | |
declare -r MY_REALNAME="$(basename "${MY_CMD}")" | |
### In help texts we might want to show the name the user used to call this script rather than | |
### its real name. | |
declare -r MY_NAME="$(basename "${0}")" | |
### Outputs an Error Message | |
function error() { | |
local msg | |
while read -r msg; do | |
echo "[ERROR] ${msg}" 1>&2 | |
done | |
} | |
function warn() { | |
local msg | |
while read -r msg; do | |
echo "[WARN] ${msg}" | |
done | |
} | |
function info() { | |
local msg | |
while read -r msg; do | |
echo "[INFO] ${msg}" | |
done | |
} | |
function parse_cli() { | |
echo "Parse CLI here." | info | |
} | |
function main() { | |
parse_cli "${@}" | |
echo "An error message" | error | |
echo "A warning" | warn | |
cat <<HELP | info | |
Some Multiline- | |
Help-Text | |
HELP | |
cat <<COLORS | info | |
${BOLD}${LRED}COLORED${NORMAL}${BOLD} OUTPUT${NORMAL} | |
${BOLD}${MY_NAME}${NORMAL} ${SMUL}command${RMUL} | |
${BOLD}${MY_NAME}${NORMAL} [${SMUL}options${RMUL}] ${SMUL}command${RMUL} | |
${BOLD}${MY_NAME}${NORMAL} [-h | --help] | |
COLORS | |
} | |
main "${@}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment