generated from the-common/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.sh
executable file
·93 lines (87 loc) · 2.41 KB
/
basic.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env bash
# _script_description_
#
# Copyright _copyright_effective_year_ _copyright_holder_name_ <_copyright_holder_contact_>
# SPDX-License-Identifier: CC-BY-SA-4.0
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
printf \
'Info: Configuring the convenience variables...\n'
if test -v BASH_SOURCE; then
# Convenience variables may not need to be referenced
# 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
# Convenience variables may not need to be referenced
# shellcheck disable=SC2034
{
script_basecommand="${0}"
script_args=("${@}")
}
printf \
'Info: Setting the ERR trap...\n'
trap_err(){
printf \
'Error: The program prematurely terminated due to an unhandled error.\n' \
1>&2
exit 99
}
if ! trap trap_err ERR; then
printf \
'Error: Unable to set the ERR trap.\n' \
1>&2
exit 1
fi
printf \
'Info: Operation completed without errors.\n'