-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathset.sh
34 lines (27 loc) · 940 Bytes
/
set.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
#!/usr/bin/env bash
# vi: ft=sh
# Utilities related to BASH's set
# This function is based on the fact that if set -e is set,
# the piped command before wc prints only "two", while if +e is set
# the overall output is "one\ntwo\n"
set-e-status() {
set -o | grep errexit | awk '{print $2}'
}
set-e-save() {
export __bash_set_errexit_status=$(mktemp -t 'errexit')
rm -f "${__bash_set_errexit_status}" 2>/dev/null
set-e-status >"${__bash_set_errexit_status}"
}
set-e-restore() {
[[ -f ${__bash_set_errexit_status} ]] && {
error "You must first save it with the function:s ${bldgrn}set-e-save"
return 1
}
local status=$(cat "${__bash_set_errexit_status}" | tr -d '\n')
if [[ ${status} != 'on' && ${status} != 'off' ]]; then
error "Invalid data in the set -e tempfile:" "$(cat "${__bash_set_errexit_status}")"
return 1
fi
set -o errexit "${status}"
rm -f "${__bash_set_errexit_status}" 2>/dev/null
}