#!/bin/sh set -eu usage() { printf "Usage: %s [-h]\n" "$0" } help() { cat << EOH SYNOPSIS $0 [-h] DESCRIPTION $0 does things! OPTIONS -h Show this help EOH } on_interrupt() { print "Process aborted!\n" exit 130 } main() { while getopts "hf:" option; do case "${option}" in h) help; exit 0;; f) FOO="$OPTARG";; ?) log -l error "$(usage)"; exit 1;; esac done FOO="${FOO:-defaultValue}" # log [-t] [-l debug|error] message exit 0 } log() { LEVEL=info TIME= while getopts "tl:" option; do case "${option}" in l) LEVEL="$OPTARG"; shift $((OPTIND-1));; t) TIME="$(printf "[%s] " "$(date +'%Y-%m-%dT%H:%m:%S.%s')")"; shift $((OPTIND-1));; *) exit 1;; esac done if [ -t 2 ] && [ -z "${NO_COLOR-}" ]; then case "${LEVEL}" in debug) COLOR="$(tput setaf 3)";; error) COLOR="$(tput setaf 1)";; *) COLOR="$(tput sgr0)" esac fi printf "%s%s%s\n" "${COLOR:-}" "${TIME:-}" "$*" >&2 } trap on_interrupt INT main "$@"