shell-base/script

72 lines
995 B
Plaintext
Raw Normal View History

2023-01-02 11:00:26 +01:00
#!/bin/sh
2023-05-21 14:56:42 +02:00
set -eu
2023-01-02 11:00:26 +01:00
usage() {
2023-01-08 15:18:23 +01:00
printf "Usage: %s [-h]\n" "$0"
}
help() {
cat << EOH
SYNOPSIS
$0 [-h]
DESCRIPTION
$0 does things!
OPTIONS
-h Show this help
EOH
2023-01-02 11:00:26 +01:00
}
on_interrupt() {
print "Process aborted!\n"
exit 130
}
main() {
while getopts "hf:" option; do
case "${option}" in
2023-01-08 15:18:23 +01:00
h) help; exit 0;;
2023-01-02 11:00:26 +01:00
f) FOO="$OPTARG";;
2023-05-21 14:56:42 +02:00
?) log -l error "$(usage)"; exit 1;;
2023-01-02 11:00:26 +01:00
esac
done
FOO="${FOO:-defaultValue}"
2023-05-21 14:57:26 +02:00
# log [-t] [-l debug|error] message
2023-05-21 14:56:42 +02:00
2023-01-02 11:00:26 +01:00
exit 0
}
2023-05-21 14:56:42 +02:00
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
}
2023-01-02 11:00:26 +01:00
trap on_interrupt INT
main "$@"