Skip to content

Commit d057cba

Browse files
committed
feat: Implement MODULAR flavor
Fixes #4. Signed-off-by: 林博仁(Buo-ren, Lin) <Buo.Ren.Lin@gmail.com>
1 parent d68b72c commit d057cba

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ Inherited from the [BARE-MINIMUM](#bare-minimum) flavor, but with the following
2828
+ `script_filename`: The full filename of the shell script
2929
+ `script_name`: The name of the shellscript, excluding the filename suffixes
3030

31+
## [MODULAR](modular.sh)
32+
33+
Inherited from the [BASIC](#basic) flavor, but with the following additions:
34+
35+
* 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.
36+
* 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.
37+
3138
## References
3239

3340
* [The Common / The Common GNU Bash Shell Script Templates · GitLab](https://gitlab.com/the-common/bash-script-templates)

modular.sh

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env bash
2+
# _script_description_
3+
#
4+
# Copyright _copyright_effective_year_ _copyright_holder_name_ <_copyright_holder_contact_>
5+
# SPDX-License-Identifier: CC-BY-SA-4.0
6+
7+
init(){
8+
printf \
9+
'Info: Operation completed without errors.\n'
10+
}
11+
12+
printf \
13+
'Info: Configuring the defensive interpreter behaviors...\n'
14+
set_opts=(
15+
# Terminate script execution when an unhandled error occurs
16+
-o errexit
17+
-o errtrace
18+
19+
# Terminate script execution when an unset parameter variable is
20+
# referenced
21+
-o nounset
22+
)
23+
if ! set "${set_opts[@]}"; then
24+
printf \
25+
'Error: Unable to configure the defensive interpreter behaviors.\n' \
26+
1>&2
27+
exit 1
28+
fi
29+
30+
printf \
31+
'Info: Checking the existence of the required commands...\n'
32+
required_commands=(
33+
realpath
34+
)
35+
flag_required_command_check_failed=false
36+
for command in "${required_commands[@]}"; do
37+
if ! command -v "${command}" >/dev/null; then
38+
flag_required_command_check_failed=true
39+
printf \
40+
'Error: This program requires the "%s" command to be available in your command search PATHs.\n' \
41+
"${command}" \
42+
1>&2
43+
fi
44+
done
45+
if test "${flag_required_command_check_failed}" == true; then
46+
printf \
47+
'Error: Required command check failed, please check your installation.\n' \
48+
1>&2
49+
exit 1
50+
fi
51+
52+
if test -v BASH_SOURCE; then
53+
printf \
54+
'Info: Configuring the convenience variables...\n'
55+
# Convenience variables
56+
# shellcheck disable=SC2034
57+
{
58+
printf \
59+
'Info: Determining the absolute path of the program...\n'
60+
if ! script="$(
61+
realpath \
62+
--strip \
63+
"${BASH_SOURCE[0]}"
64+
)"; then
65+
printf \
66+
'Error: Unable to determine the absolute path of the program.\n' \
67+
1>&2
68+
exit 1
69+
fi
70+
script_dir="${script%/*}"
71+
script_filename="${script##*/}"
72+
script_name="${script_filename%%.*}"
73+
}
74+
fi
75+
76+
trap_err(){
77+
printf \
78+
'Error: The program has encountered an unhandled error and is prematurely aborted.\n' \
79+
1>&2
80+
}
81+
82+
printf \
83+
'Info: Setting the ERR trap...\n'
84+
if ! trap trap_err ERR; then
85+
printf \
86+
'Error: Unable to set the ERR trap.\n' \
87+
1>&2
88+
exit 1
89+
fi
90+
91+
init

0 commit comments

Comments
 (0)