-
Notifications
You must be signed in to change notification settings - Fork 156
/
script.sh
executable file
·95 lines (84 loc) · 2.79 KB
/
script.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
94
95
#!/usr/bin/env bash
# A best practices Bash script template with many useful functions. This file
# sources in the bulk of the functions from the source.sh file which it expects
# to be in the same directory. Only those functions which are likely to need
# modification are present in this file. This is a great combination if you're
# writing several scripts! By pulling in the common functions you'll minimise
# code duplication, as well as ease any potential updates to shared functions.
# Enable xtrace if the DEBUG environment variable is set
if [[ ${DEBUG-} =~ ^1|yes|true$ ]]; then
set -o xtrace # Trace the execution of the script (debug)
fi
# Only enable these shell behaviours if we're not being sourced
# Approach via: https://stackoverflow.com/a/28776166/8787985
if ! (return 0 2> /dev/null); then
# A better class of script...
set -o errexit # Exit on most errors (see the manual)
set -o nounset # Disallow expansion of unset variables
set -o pipefail # Use last non-zero exit code in a pipeline
fi
# Enable errtrace or the error trap handler will not work as expected
set -o errtrace # Ensure the error trap handler is inherited
# DESC: Usage help
# ARGS: None
# OUTS: None
# RETS: None
function script_usage() {
cat << EOF
Usage:
-h|--help Displays this help
-v|--verbose Displays verbose output
-nc|--no-colour Disables colour output
-cr|--cron Run silently unless we encounter an error
EOF
}
# DESC: Parameter parser
# ARGS: $@ (optional): Arguments provided to the script
# OUTS: Variables indicating command-line parameters and options
# RETS: None
function parse_params() {
local param
while [[ $# -gt 0 ]]; do
param="$1"
shift
case $param in
-h | --help)
script_usage
exit 0
;;
-v | --verbose)
verbose=true
;;
-nc | --no-colour)
no_colour=true
;;
-cr | --cron)
cron=true
;;
*)
script_exit "Invalid parameter was provided: $param" 1
;;
esac
done
}
# DESC: Main control flow
# ARGS: $@ (optional): Arguments provided to the script
# OUTS: None
# RETS: None
function main() {
trap script_trap_err ERR
trap script_trap_exit EXIT
script_init "$@"
parse_params "$@"
cron_init
colour_init
#lock_init system
}
# shellcheck source=source.sh
source "$(dirname "${BASH_SOURCE[0]}")/source.sh"
# Invoke main with args if not sourced
# Approach via: https://stackoverflow.com/a/28776166/8787985
if ! (return 0 2> /dev/null); then
main "$@"
fi
# vim: syntax=sh cc=80 tw=79 ts=4 sw=4 sts=4 et sr