@@ -14,19 +14,46 @@ set -o pipefail # Use last non-zero exit code in a pipeline
1414# DESC: Handler for unexpected errors
1515# ARGS: $1 (optional): Exit code (defaults to 1)
1616function script_trap_err() {
17+ local exit_code=1
18+
1719 # Disable the error trap handler to prevent potential recursion
1820 trap - ERR
1921
2022 # Consider any further errors non-fatal to ensure we run to completion
2123 set +o errexit
2224 set +o pipefail
2325
24- # Exit with failure status
26+ # Validate any provided exit code
2527 if [[ $# -eq 1 && $1 =~ ^[0-9]+$ ]]; then
26- exit " $1 "
27- else
28- exit 1
28+ exit_code=" $1 "
2929 fi
30+
31+ # Output debug data if in Cron mode
32+ if [[ -n ${cron-} ]]; then
33+ # Restore original file output descriptors
34+ if [[ -n ${script_output-} ]]; then
35+ exec 1>&3 2>&4
36+ fi
37+
38+ # Print basic debugging information
39+ printf ' %b\n' " $ta_none "
40+ printf ' ***** Abnormal termination of script *****\n'
41+ printf ' Script Path: %s\n' " $script_path "
42+ printf ' Script Parameters: %s\n' " $script_params "
43+ printf ' Script Exit Code: %s\n' " $exit_code "
44+
45+ # Print the script log if we have it. It's possible we may not if we
46+ # failed before we even called cron_init(). This can happen if bad
47+ # parameters were passed to the script so we bailed out very early.
48+ if [[ -n ${script_output-} ]]; then
49+ printf ' Script Output:\n\n%s' " $( cat " $script_output " ) "
50+ else
51+ printf ' Script Output: None (failed before log init)\n'
52+ fi
53+ fi
54+
55+ # Exit with failure status
56+ exit " $exit_code "
3057}
3158
3259
@@ -35,6 +62,11 @@ function script_trap_err() {
3562function script_trap_exit() {
3663 cd " $orig_cwd "
3764
65+ # Remove Cron mode script log
66+ if [[ -n ${cron-} && -f ${script_output-} ]]; then
67+ rm " $script_output "
68+ fi
69+
3870 # Restore terminal colours
3971 printf ' %b' " $ta_none "
4072}
@@ -64,13 +96,14 @@ function script_exit() {
6496
6597
6698# DESC: Generic script initialisation
67- # ARGS: None
99+ # ARGS: $@ (optional): Arguments provided to the script
68100function script_init() {
69101 # Useful paths
70102 readonly orig_cwd=" $PWD "
71103 readonly script_path=" ${BASH_SOURCE[0]} "
72104 readonly script_dir=" $( dirname " $script_path " ) "
73105 readonly script_name=" $( basename " $script_path " ) "
106+ readonly script_params=" $* "
74107
75108 # Important to always set as we use it in the exit handler
76109 readonly ta_none=" $( tput sgr0 || true) "
@@ -159,6 +192,17 @@ function colour_init() {
159192}
160193
161194
195+ # DESC: Initialise Cron mode
196+ # ARGS: None
197+ function cron_init() {
198+ if [[ -n ${cron-} ]]; then
199+ # Redirect all output to a temporary file
200+ readonly script_output=" $( mktemp --tmpdir " $script_name " .XXXXX) "
201+ exec 3>&1 4>&2 1> " $script_output " 2>&1
202+ fi
203+ }
204+
205+
162206# DESC: Pretty print the provided string
163207# ARGS: $1 (required): Message to print (defaults to a green foreground)
164208# $2 (optional): Colour to print the message with. This can be an ANSI
@@ -317,6 +361,7 @@ Usage:
317361 -h|--help Displays this help
318362 -v|--verbose Displays verbose output
319363 -nc|--no-colour Disables colour output
364+ -cr|--cron Run silently unless we encounter an error
320365EOF
321366}
322367
@@ -339,6 +384,9 @@ function parse_params() {
339384 -nc|--no-colour)
340385 no_colour=" true"
341386 ;;
387+ -cr|--cron)
388+ cron=" true"
389+ ;;
342390 * )
343391 script_exit " Invalid parameter was provided: $param " 2
344392 ;;
@@ -353,8 +401,9 @@ function main() {
353401 trap " script_trap_err" ERR
354402 trap " script_trap_exit" EXIT
355403
356- script_init
404+ script_init " $@ "
357405 parse_params " $@ "
406+ cron_init
358407 colour_init
359408}
360409
0 commit comments