Last active
July 6, 2021 19:17
-
-
Save djeikyb/106327707a80a1b7aff0510a0ef519b1 to your computer and use it in GitHub Desktop.
common shell functions, a skeleton for new scripts
This file contains hidden or 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
SELF=${0##*/} | |
die() { | |
log "$@" | |
exit 1 | |
} | |
log() { | |
printf "$SELF: %s\n" "$@" >&2 | |
} | |
usage() { | |
cat <<EOF | |
usage: $self [--help] | |
Examples: | |
$SELF --help | |
EOF | |
} | |
requireCommand() { | |
command -v $1 >/dev/null 2>&1 || die "'$1' is required, but not found" | |
} | |
while [ $# -gt 0 ]; do | |
case "$1" in | |
"") | |
usage | |
die "unrecognized argument: $1" | |
exit 1 | |
;; | |
--help|help|-help|-h) | |
usage | |
exit 0 | |
;; | |
somearg|-a) | |
shift | |
;; | |
*) | |
die "unrecognized argument: $1" | |
;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment