Skip to content

Commit

Permalink
feat: Implement MODULAR flavor
Browse files Browse the repository at this point in the history
Signed-off-by: 林博仁(Buo-ren, Lin) <[email protected]>
  • Loading branch information
brlin-tw committed Mar 23, 2024
1 parent d68b72c commit 52338b4
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ Inherited from the [BARE-MINIMUM](#bare-minimum) flavor, but with the following
+ `script_filename`: The full filename of the shell script
+ `script_name`: The name of the shellscript, excluding the filename suffixes

## [MODULAR](modular.sh)

Inherited from the [BASIC](#basic) flavor, but with the following additions:

* The introduction of the `init` function, which enables the moving of the program's main logic to the start of the script file, increases readability.
* A new `trap_err` function has being implemented to handle the ERR trap so one can immediately recognize that an error has occurred when the `errexit` interpreter behavior is triggered.

## References

* [The Common / The Common GNU Bash Shell Script Templates · GitLab](https://gitlab.com/the-common/bash-script-templates)
Expand Down
91 changes: 91 additions & 0 deletions modular.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env bash
# _script_description_
#
# Copyright _copyright_effective_year_ _copyright_holder_name_ <_copyright_holder_contact_>
# SPDX-License-Identifier: CC-BY-SA-4.0

init(){
printf \
'Info: Operation completed without errors.\n'
}

printf \
'Info: Configuring the defensive interpreter behaviors...\n'
set_opts=(
# Terminate script execution when an unhandled error occurs
-o errexit
-o errtrace

# Terminate script execution when an unset parameter variable is
# referenced
-o nounset
)
if ! set "${set_opts[@]}"; then
printf \
'Error: Unable to configure the defensive interpreter behaviors.\n' \
1>&2
exit 1
fi

printf \
'Info: Checking the existence of the required commands...\n'
required_commands=(
realpath
)
flag_required_command_check_failed=false
for command in "${required_commands[@]}"; do
if ! command -v "${command}" >/dev/null; then
flag_required_command_check_failed=true
printf \
'Error: This program requires the "%s" command to be available in your command search PATHs.\n' \
"${command}" \
1>&2
fi
done
if test "${flag_required_command_check_failed}" == true; then
printf \
'Error: Required command check failed, please check your installation.\n' \
1>&2
exit 1
fi

if test -v BASH_SOURCE; then
printf \
'Info: Configuring the convenience variables...\n'
# Convenience variables
# shellcheck disable=SC2034
{
printf \
'Info: Determining the absolute path of the program...\n'
if ! script="$(
realpath \
--strip \
"${BASH_SOURCE[0]}"
)"; then
printf \
'Error: Unable to determine the absolute path of the program.\n' \
1>&2
exit 1
fi
script_dir="${script%/*}"
script_filename="${script##*/}"
script_name="${script_filename%%.*}"
}
fi

trap_err(){
printf \
'Error: The program has encountered an unhandled error and is prematurely aborted.\n' \
1>&2
}

printf \
'Info: Setting the ERR trap...\n'
if ! trap trap_err ERR; then
printf \
'Error: Unable to set the ERR trap.\n' \
1>&2
exit 1
fi

init

0 comments on commit 52338b4

Please sign in to comment.