Skip to content

Commit c2d6770

Browse files
author
Alessio Treglia
committed
improve error handling
1 parent ada3bd8 commit c2d6770

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

bashrcd

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ BASHRCD_VERSION='0.1.1'
2626

2727
g_bashrcd_local_symbols=''
2828

29+
trap 'echo "aborted (rc = $?)" >&2' EXIT
2930

3031
f_bashrcd_register_local_symbol() {
3132
g_bashrcd_local_symbols="${g_bashrcd_local_symbols} $1"
@@ -39,7 +40,7 @@ f_bashrcd_abort() {
3940
l_rc="$1"
4041
l_err="$2"
4142

42-
[[ -z "${l_err}" ]] || printf "%s" "${l_err}\n" >&2
43+
[[ -z "${l_err}" ]] || printf "%s\n" "${l_err}" >&2
4344

4445
exit "${l_rc}"
4546
}
@@ -81,6 +82,7 @@ f_bashrcd_check_run_parts() {
8182
l_cmd_busybox="$(which busybox 2>/dev/null)"
8283
l_rc="$?"
8384
if [ "${l_rc}" -eq 0 ]; then
85+
l_rc=''
8486
${l_cmd_busybox} --list | grep -q run-parts 2>/dev/null
8587
l_rc="$?"
8688
[ "${l_rc}" -eq 0 ] && printf "%s" "${l_cmd_busybox} run-parts" && return 0
@@ -91,7 +93,7 @@ f_bashrcd_check_run_parts() {
9193
l_rc="$?"
9294
[ "${l_rc}" -eq 0 ] && printf "%s" "${l_cmd_run_parts}" && return 0
9395

94-
f_bashrcd_abort 2 "Unable to locate run-parts"
96+
f_bashrcd_abort 10 "f_bashrcd_check_run_parts: coudln't find run-parts"
9597
}
9698
f_bashrcd_register_local_symbol f_bashrcd_check_run_parts
9799

@@ -162,6 +164,9 @@ f_bashrcd_register_local_symbol f_bashrcd_fatal_arg_not_found
162164

163165
# main()
164166

167+
# This program can't run without run-parts or a compatible implementation
168+
f_bashrcd_check_run_parts
169+
165170
f_bashrcd_init_userdir
166171

167172
# shellcheck disable=SC1001
@@ -172,7 +177,7 @@ while getopts ":d:hnv" opt; do
172177
n) f_bashrcd_list_scripts ; exit 0 ;;
173178
v) f_bashrcd_version ; exit 0 ;;
174179
\:) f_bashrcd_fatal_arg_not_found "${OPTARG}" ; f_bashrcd_abort 1 ;;
175-
\?) f_bashrcd_fatal_invalid_option "${OPTARG}" ; f_bashrcd_abort 1 ;;
180+
\?) f_bashrcd_fatal_invalid_option "${OPTARG}" ; f_bashrcd_abort 2 ;;
176181
esac
177182
done
178183

0 commit comments

Comments
 (0)