vim/templates/skeleton.sh

212 lines
4.8 KiB
Bash
Raw Normal View History

2022-02-03 14:32:08 +01:00
#!/bin/sh
#
# Purpose {{{
# This script will …
# 1. …
# …
#
# !!DATE!!
# }}}
2023-01-24 18:30:20 +01:00
# Flags {{{
## Exit on error {{{
set -o errexit
## }}}
## Exit on unset var {{{
### Use "${VARNAME-}" to test a var that may not have been set
set -o nounset
## }}}
## Pipeline command is treated as failed {{{
### Not available in POSIX sh https://github.com/koalaman/shellcheck/wiki/SC3040
#set -o pipefail
## }}}
## Help with debugging {{{
### Call the script by prefixing it with "TRACE=1 ./script.sh"
if [ "${TRACE-0}" -eq 1 ]; then set -o xtrace; fi
## }}}
# }}}
2022-02-03 14:32:08 +01:00
# Vars {{{
2022-07-19 11:33:20 +02:00
PROGNAME=$(basename "${0}"); readonly PROGNAME
2023-01-24 18:30:20 +01:00
PROGDIR=$(readlink --canonicalize-missing $(dirname "${0}")); readonly PROGDIR
2022-07-19 11:33:20 +02:00
ARGS="${*}"; readonly ARGS
2022-02-03 14:32:08 +01:00
readonly NBARGS="${#}"
2023-01-24 18:30:20 +01:00
[ -z "${DEBUG-}" ] && DEBUG=1
2022-02-03 14:32:08 +01:00
## Export DEBUG for sub-script
export DEBUG
## Default values for some vars
#readonly MY_VAR_XY_DEFAULT="666"
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
# }}}
usage() { # {{{
cat <<- HELP
usage: $PROGNAME [-d|-h]
Try to give a description…
EXAMPLES:
- Apply my script to…
${PROGNAME}
OPTIONS:
-d,--debug
Enable debug messages.
-h,--help
Print this help message.
HELP
}
# }}}
debug_message() { # {{{
local_debug_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_debug_message}"
unset local_debug_message
return 0
}
# }}}
error_message() { # {{{
local_error_message="${1}"
local_error_code="${2}"
## Print message
2023-01-24 18:30:20 +01:00
printf '%b\n' "ERROR ${PROGNAME}: ${RED}${local_error_message}${RESET}" >&2
unset local_error_message
2022-02-03 14:32:08 +01:00
exit "${local_error_code:=66}"
}
# }}}
define_vars() { # {{{
## If my_var_xy wasn't defined (argument) {{{
2023-01-24 18:30:20 +01:00
#if [ -z "${my_var_xy-}" ]; then
2022-02-03 14:32:08 +01:00
### Use default value
#readonly my_var_xy="${MY_VAR_XY_DEFAULT}"
#fi
## }}}
2022-12-01 10:40:38 +01:00
true; ## Remove me
2022-02-03 14:32:08 +01:00
}
# }}}
2023-04-28 14:48:32 +02:00
is_command_available() { # {{{
local_command_available_cmd="${1}"
debug_prefix="${2:-}"
## Return False by default
return_command_available="1"
if [ "$(command -v ${local_command_available_cmd})" ]; then
debug_message "${debug_prefix}is_command_available \
${RED}${local_command_available_cmd}${COLOR_DEBUG} seems present on this host."
return_command_available="0"
else
debug_message "${debug_prefix}is_command_available \
${RED}${local_command_available_cmd}${COLOR_DEBUG} is not available on this host."
return_command_available="1"
fi
unset local_command_available_cmd
unset debug_prefix
return "${return_command_available}"
}
# }}}
2022-02-03 14:32:08 +01:00
main() { # {{{
2022-12-01 10:40:38 +01:00
debug_message "--- MAIN BEGIN"
2023-04-28 14:48:32 +02:00
## If a command is missing {{{
### Exit with error message
is_command_available "test_me" "| " \
|| error_message "No test_me command available. Please install test_me package with your package manager." 01
2022-02-03 14:32:08 +01:00
## }}}
## Define all vars
define_vars
2022-12-01 10:40:38 +01:00
debug_message "| Define vars"
2022-02-03 14:32:08 +01:00
2022-12-01 10:40:38 +01:00
debug_message "--- MAIN END"
2022-02-03 14:32:08 +01:00
}
# }}}
# Manage arguments # {{{
# This code can't be in a function due to argument management
if [ ! "${NBARGS}" -eq "0" ]; then
manage_arg="0"
2023-01-24 18:30:20 +01:00
## If the first argument ask for help (h|help|-h|-help|-*h|-*help) {{{
if printf -- '%s' "${1-}" | grep --quiet --extended-regexp -- "^-*h(elp)?$"; then
usage
exit 0
fi
## }}}
2022-02-03 14:32:08 +01:00
## If the first argument is not an option
2023-01-24 18:30:20 +01:00
if ! printf -- '%s' "${1}" | grep --quiet --extended-regexp -- "^-+";
2022-02-03 14:32:08 +01:00
then
## Print help message and exit
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
fi
# Parse all options (start with a "-") one by one
2023-01-24 18:30:20 +01:00
while printf -- '%s' "${1-}" | grep --quiet --extended-regexp -- "^-+"; do
2022-02-03 14:32:08 +01:00
case "${1}" in
-d|--debug ) ## debug
DEBUG=0
2022-12-01 10:40:38 +01:00
debug_message "--- Manage argument BEGIN"
2022-02-03 14:32:08 +01:00
;;
#-v|--var ) ## Define var with given arg
### Move to the next argument
#shift
### Define var
#readonly my_var_xy="${1}"
#;;
* ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
2022-12-01 10:40:38 +01:00
debug_message "| ${RED}${1}${COLOR_DEBUG} option managed."
2022-02-03 14:32:08 +01:00
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
2022-12-01 10:40:38 +01:00
debug_message "| ${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
2022-02-03 14:32:08 +01:00
else
2022-12-01 10:40:38 +01:00
debug_message "| No arguments/options to manage."
2022-02-03 14:32:08 +01:00
fi
2022-12-01 10:40:38 +01:00
debug_message "--- Manage argument END"
2022-02-03 14:32:08 +01:00
# }}}
main
exit 255