Skip to content

Instantly share code, notes, and snippets.

@nxnev

nxnev/example.bash Secret

Created Dec 10, 2017
Embed
What would you like to do?
#!/usr/bin/env bash
#===============================================================================
args=()
opts=( 'a' 'b' 'c' )
verbose='no'
version='1.0'
a='no'
b='no'
c=''
#===============================================================================
main() {
show_input
so 'This message is sent to stdout'
se 'This message is sent to stderr'
ok 'This is a success message'
wrn 'This is a warning message'
err 'This is an error message'
dbg 'This is a debug message'
}
#===============================================================================
show_input() {
local 'arg' 'opt'
if (( "${#opts[@]}" > 0 )); then
dbg 'Options:'
for opt in "${opts[@]}"; do
dbg " ${opt}: [${!opt}]"
done
fi
if (( "${#args[@]}" > 0 )); then
dbg 'Arguments:'
for (( arg = 1; arg <= "${#args[@]}"; arg++ )); do
dbg " ${arg}: [${args[arg-1]}]"
done
fi
}
#-------------------------------------------------------------------------------
so() { printf '%s\n' "${@}"; }
se() { printf '%s\n' "${@}" >&2; }
ok() { printf '\e[1;32m[OK]\e[0m %s\n' "${@}" >&2; }
wrn() { printf '\e[1;33m[WARNING]\e[0m %s\n' "${@}" >&2; }
err() { printf '\e[1;31m[ERROR]\e[0m %s\n' "${@}" >&2; }
dbg() { if [[ "${verbose}" == 'yes' ]]; then dbg() { printf \
'\e[1;34m[DEBUG]\e[0m %s\n' "${@}" >&2; }; dbg "${@}"; else dbg() { :; }; fi; }
#-------------------------------------------------------------------------------
show_help() {
cat << 'EOF'
Usage: script [--help|--version] [--verbose] [-a] [-b] [-c] [--] [operands]
EOF
}
#===============================================================================
while [[ "${1++}" ]]; do
case "${1}" in
( '-a' )
a='yes'
;;
( '-b' )
b='yes'
;;
( '-c' )
c="${2}"
shift
;;
( '--help' | '-h' )
show_help
exit
;;
( '--verbose' | '-v' )
verbose='yes'
;;
( '--version' | '-V' )
so "${version}"
exit
;;
( '--' )
shift
args+=( "${@}" )
break
;;
( * )
args+=( "${1}" )
;;
esac
shift || break
done
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment