added template.sh
This commit is contained in:
parent
da1bb6472f
commit
99539d54aa
|
@ -0,0 +1,139 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
|
||||
## ## ## ## ## ## ## ## ## ## ## ## ## ##
|
||||
## ##
|
||||
## template.sh ##
|
||||
## ##
|
||||
## Template for creating BASH scripts ##
|
||||
## ##
|
||||
## ## ## ## ## ## ## ## ## ## ## ## ## ##
|
||||
|
||||
|
||||
##
|
||||
## Information
|
||||
## (don't touch unless you are me)
|
||||
##
|
||||
|
||||
NAME="template.sh"
|
||||
DESCRIPTION="Template for creating BASH scripts"
|
||||
VERSION="0.1.1"
|
||||
AUTHOR="david@socialnerds.org"
|
||||
LICENSE="MIT"
|
||||
WEBSITE="https://git.socialnerds.org/david/scripts"
|
||||
CHANGELOG=("[2023-10-26][v0.1.1] Everything is a work in progress"
|
||||
"[2023-10-25][v0.1.0] Initial version")
|
||||
|
||||
|
||||
##
|
||||
## Configuration
|
||||
##
|
||||
|
||||
EXECUTABLE="$(basename $0)"
|
||||
LIBRARIES="lib.sh"
|
||||
DEPENDENCIES="sed awk jq"
|
||||
REQUIRE_ROOT=0
|
||||
|
||||
|
||||
##
|
||||
## Functions
|
||||
##
|
||||
|
||||
# Print help information
|
||||
function print_help() {
|
||||
printf "%s\n\n%s\n%b\n\n%s\n %-15s %s\n %-15s %s\n %-15s %s\n %-15s %s\n" \
|
||||
"$DESCRIPTION" "Usage:" "$LIB_BOLD$EXECUTABLE <options>$LIB_CLEAR" "Options:" \
|
||||
"-h, --help" "Print help screen and exit" \
|
||||
"-i, --info" "Print script information and exit" \
|
||||
"-v, --verbose" "More verbose output" \
|
||||
"-q, --quiet" "No output except errors (overrides -v)"
|
||||
}
|
||||
|
||||
|
||||
##
|
||||
## Preflight
|
||||
##
|
||||
|
||||
# Load BASH libraries
|
||||
SCRIPT_PATH=$(readlink -f "$0")
|
||||
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
|
||||
for LIBRARY in $LIBRARIES; do
|
||||
if [[ -r "$SCRIPT_DIR/$LIBRARY" ]]; then
|
||||
#echo "Loading library file [$SCRIPT_DIR/$LIBRARY]"
|
||||
source "$SCRIPT_DIR/$LIBRARY"
|
||||
else
|
||||
echo "Error: Cannot load library file [$SCRIPT_DIR/$LIBRARY]"
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
# Check for root privileges
|
||||
if ! lib_amiroot && [[ $REQUIRE_ROOT -eq 1 ]]; then
|
||||
lib_print "!You need to have root privileges"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check for dependencies
|
||||
MISSING_COMMANDS=$(lib_missing_commands $DEPENDENCIES)
|
||||
if [[ -n "$MISSING_COMMANDS" ]]; then
|
||||
lib_print "!One or more commands missing [$MISSING_COMMANDS]"
|
||||
lib_print "Try installing them with your package manager"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
##
|
||||
## Liftoff
|
||||
##
|
||||
|
||||
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do
|
||||
case $1 in
|
||||
-h|--help)
|
||||
H=1
|
||||
;;
|
||||
-i|--info)
|
||||
I=1
|
||||
;;
|
||||
-v|--verbose)
|
||||
V=1
|
||||
;;
|
||||
-q|--quiet)
|
||||
Q=1
|
||||
;;
|
||||
*)
|
||||
lib_print "!Unknown option [$1]"
|
||||
lib_print "Try --help or -h for available options"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
if [[ "$1" == '--' ]]; then
|
||||
shift
|
||||
fi
|
||||
|
||||
if [[ $I -eq 1 ]]; then
|
||||
lib_print_info
|
||||
exit 0
|
||||
elif [[ $H -eq 1 ]]; then
|
||||
print_help
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ -z "$1" ]]; then
|
||||
print_help
|
||||
exit 0
|
||||
else
|
||||
lib_print "Supplied option: $1"
|
||||
lib_print "!super error"
|
||||
lib_print "?super test"
|
||||
lib_print "$(lib_gen_string 64)"
|
||||
fi
|
||||
|
||||
|
||||
##
|
||||
## Here be dragons
|
||||
##
|
||||
|
||||
exit 0
|
Loading…
Reference in New Issue