-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_init.sh
More file actions
57 lines (52 loc) · 1.75 KB
/
lib_init.sh
File metadata and controls
57 lines (52 loc) · 1.75 KB
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
#!/usr/bin/env bash
# ==============================================================================
###
### # lib_init.sh
###
### A file for some initial Bash Scripting best practices. It applies the
### following settings:
###
### * `errexit`: Abort on nonzero exit status
### * `nounset`: Abort on unbound variable
### * `pipefail`: Don't hide errors within pipes
### * `xtrace`: Show expanded commands if `DEBUG` is set to 2. Set to 1 for
### more verbose output.
###
### **Usage**:
###
### ```bash
### MY_PATH="$(realpath "${BASH_SOURCE[0]}")"
### SCRIPT_DIR="$(dirname "${MY_PATH}")"
### readonly MY_PATH
### readonly SCRIPT_DIR
### readonly LIB_BASH_DIR="${SCRIPT_DIR}/lib_bash"
### source "${LIB_BASH_DIR}/lib_init.sh"
### ```
###
# ==============================================================================
# Guard variable to prevent multiple imports
if [[ -n "${LIB_INIT_SH_INCLUDED:-}" ]]; then
return
fi
LIB_INIT_SH_INCLUDED=1
# ------------------------------------------------------------------------------
###
### ## Constants
###
# ------------------------------------------------------------------------------
### * `DEBUG`: Provide option to trigger debug output with different verbosity
### levels.
declare -ri DEBUG=${DEBUG:-0}
export DEBUG
# ------------------------------------------------------------------------------
# Bash Options
# ------------------------------------------------------------------------------
set -o errexit # abort on nonzero exit status
set -o nounset # abort on unbound variable
set -o pipefail # don't hide errors within pipes
### Call with `DEBUG=2 <command>.sh <file>` to enable verbose debug output
if ((DEBUG > 1)); then
set -o xtrace # show expanded commands
else
set +o xtrace # do not show expanded commands
fi