Skip to content

Instantly share code, notes, and snippets.

@aakropotkin
Created May 1, 2023 16:54
Show Gist options
  • Save aakropotkin/901e5788443119aeaa2b05999eb8f93a to your computer and use it in GitHub Desktop.
Save aakropotkin/901e5788443119aeaa2b05999eb8f93a to your computer and use it in GitHub Desktop.
A starter bash script. Just change `FILE` and fill in the rest
#! /usr/bin/env bash
# ============================================================================ #
#
#
#
# ---------------------------------------------------------------------------- #
set -eu;
set -o pipefail;
# ---------------------------------------------------------------------------- #
_as_me="FILE.sh";
_version="0.1.0";
_usage_msg="USAGE: $_as_me [OPTIONS...]
";
_help_msg="$_usage_msg
OPTIONS
-h,--help Print help message to STDOUT.
-u,--usage Print usage message to STDOUT.
-v,--version Print version information to STDOUT.
ENVIRONMENT
GREP Command used as \`grep' executable.
REALPATH Command used as \`realpath' executable.
MKTEMP Command used as \`mktemp' executable.
";
# ---------------------------------------------------------------------------- #
usage() {
if [[ "${1:-}" = "-f" ]]; then
echo "$_help_msg";
else
echo "$_usage_msg";
fi
}
# ---------------------------------------------------------------------------- #
# @BEGIN_INJECT_UTILS@
: "${GREP:=grep}";
: "${REALPATH:=realpath}";
: "${MKTEMP:=mktemp}";
# ---------------------------------------------------------------------------- #
declare -a tmp_files tmp_dirs;
tmp_files=();
tmp_dirs=();
mktmp_auto() {
local _f;
_f="$( $MKTEMP "$@"; )";
case " $* " in
*\ -d\ *|*\ --directory\ *) tmp_dirs+=( "$_f" ); ;;
*) tmp_files+=( "$_f" ); ;;
esac
echo "$_f";
}
# ---------------------------------------------------------------------------- #
cleanup() {
rm -f "${tmp_files[@]}";
rm -rf "${tmp_dirs[@]}";
}
_es=0;
trap '_es="$?"; cleanup; exit "$_es";' HUP TERM INT QUIT EXIT;
# ---------------------------------------------------------------------------- #
while [[ "$#" -gt 0 ]]; do
case "$1" in
# Split short options such as `-abc' -> `-a -b -c'
-[^-]?*)
_arg="$1";
declare -a _args;
_args=();
shift;
_i=1;
while [[ "$_i" -lt "${#_arg}" ]]; do
_args+=( "-${_arg:$_i:1}" );
_i="$(( _i + 1 ))";
done
set -- "${_args[@]}" "$@";
unset _arg _args _i;
continue;
;;
--*=*)
_arg="$1";
shift;
set -- "${_arg%%=*}" "${_arg#*=}" "$@";
unset _arg;
continue;
;;
-u|--usage) usage; exit 0; ;;
-h|--help) usage -f; exit 0; ;;
-v|--version) echo "$_version"; exit 0; ;;
--) shift; break; ;;
-?|--*)
echo "$_as_me: Unrecognized option: '$1'" >&2;
usage -f >&2;
exit 1;
;;
*)
echo "$_as_me: Unexpected argument(s) '$*'" >&2;
usage -f >&2;
exit 1;
;;
esac
shift;
done
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
#
#
#
# ============================================================================ #
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment