1 |
8291
|
aaronmk
|
#!/bin/bash -e
|
2 |
8952
|
aaronmk
|
set -o errexit # in case caller did not have -e in #! line
|
3 |
|
|
|
4 |
9018
|
aaronmk
|
if test ! "$_util_sh_include_guard_utils"; then
|
5 |
|
|
_util_sh_include_guard_utils=1
|
6 |
|
|
|
7 |
8957
|
aaronmk
|
function extern () { (exec "$@") || return; }
|
8 |
8917
|
aaronmk
|
|
9 |
9002
|
aaronmk
|
isset () { test -n "${!1+isset}"; }
|
10 |
|
|
|
11 |
8716
|
aaronmk
|
realpath () { readlink -f -- "$1"; }
|
12 |
8703
|
aaronmk
|
|
13 |
8917
|
aaronmk
|
include_guard_var () { realpath "$1"|"extern" sed 's/[^a-zA-Z0-9_]/_/g'; }
|
14 |
8716
|
aaronmk
|
|
15 |
8703
|
aaronmk
|
self_not_included () # usage: if self_not_included; then ... fi
|
16 |
|
|
{
|
17 |
8971
|
aaronmk
|
test $# -ge 1 || set -- "${BASH_SOURCE[1]}"
|
18 |
8703
|
aaronmk
|
local include_guard="$(include_guard_var "$1")"
|
19 |
8793
|
aaronmk
|
alias self_being_included=false
|
20 |
9003
|
aaronmk
|
! isset "$include_guard" && \
|
21 |
8793
|
aaronmk
|
{ eval "$include_guard"=1; alias self_being_included=true; }
|
22 |
8703
|
aaronmk
|
}
|
23 |
|
|
|
24 |
8793
|
aaronmk
|
# to load newly-defined aliases for use in functions in the same file:
|
25 |
|
|
## fi # load new aliases
|
26 |
|
|
## if self_being_included; then
|
27 |
|
|
# this is needed because aliases defined inside an if statement are not
|
28 |
|
|
# available inside that if statement
|
29 |
|
|
|
30 |
9018
|
aaronmk
|
fi
|
31 |
9017
|
aaronmk
|
|
32 |
9018
|
aaronmk
|
|
33 |
8704
|
aaronmk
|
if self_not_included "${BASH_SOURCE[0]}"; then
|
34 |
|
|
|
35 |
9017
|
aaronmk
|
|
36 |
|
|
#### options
|
37 |
|
|
|
38 |
8709
|
aaronmk
|
shopt -s expand_aliases
|
39 |
|
|
|
40 |
9017
|
aaronmk
|
|
41 |
|
|
#### aliases
|
42 |
|
|
|
43 |
8889
|
aaronmk
|
unalias () { builtin unalias "$@" 2>&- || true; } # no error if undefined
|
44 |
|
|
|
45 |
9017
|
aaronmk
|
|
46 |
8886
|
aaronmk
|
#### exceptions
|
47 |
|
|
|
48 |
8976
|
aaronmk
|
alias rethrow='return "$e"'
|
49 |
|
|
alias rethrow_subshell='exit "$e"'
|
50 |
|
|
|
51 |
8978
|
aaronmk
|
fi # load new aliases
|
52 |
|
|
if self_being_included; then
|
53 |
|
|
|
54 |
|
|
# usage: save_e cmd... || use $e (or $?)
|
55 |
|
|
function save_e () { "$@"; e=$?; rethrow; } # rethrow because e= clears $?
|
56 |
|
|
alias save_e='declare e; save_e ' # last space alias-expands next word
|
57 |
|
|
|
58 |
8981
|
aaronmk
|
function log_e () # usage: log_e cmd... [ || use $e (or $?) ]
|
59 |
|
|
{
|
60 |
8982
|
aaronmk
|
if ! "save_e" "$@"; then
|
61 |
8981
|
aaronmk
|
echo "! $*" >&2
|
62 |
|
|
echo "! command exited with error $e" >&2
|
63 |
|
|
rethrow
|
64 |
|
|
fi
|
65 |
|
|
}
|
66 |
8982
|
aaronmk
|
alias log_e='declare e; log_e ' # last space alias-expands next word
|
67 |
8981
|
aaronmk
|
|
68 |
8886
|
aaronmk
|
# usage: try cmd...; ignore status; if catch status; then ...; fi; end_try
|
69 |
|
|
|
70 |
8980
|
aaronmk
|
function try () { "save_e" "$@" || true; }
|
71 |
|
|
alias try='declare e; try ' # last space alias-expands next word
|
72 |
8886
|
aaronmk
|
|
73 |
|
|
catch () { test "$e" -eq "$1"; e=0; }
|
74 |
|
|
|
75 |
|
|
ignore () { catch "$@" || true; }
|
76 |
|
|
|
77 |
8977
|
aaronmk
|
alias end_try='rethrow'
|
78 |
|
|
alias end_try_subshell='rethrow_subshell'
|
79 |
8886
|
aaronmk
|
|
80 |
9017
|
aaronmk
|
|
81 |
8899
|
aaronmk
|
#### integers
|
82 |
|
|
|
83 |
|
|
let () { builtin let "$@" || true; }
|
84 |
|
|
# "If the last ARG evaluates to 0, let returns 1" (`help let`)
|
85 |
|
|
|
86 |
9004
|
aaronmk
|
bool2int () { save_e test -z "$1"; echo "$e"; } # empty->0; non-empty->1
|
87 |
|
|
|
88 |
9017
|
aaronmk
|
|
89 |
8854
|
aaronmk
|
#### arrays
|
90 |
8694
|
aaronmk
|
|
91 |
8833
|
aaronmk
|
join () { local IFS="$delim"; echo "$*"; } # usage: delim=... join elems...
|
92 |
8816
|
aaronmk
|
|
93 |
8691
|
aaronmk
|
reverse () # usage: array=($(reverse args...))
|
94 |
|
|
{
|
95 |
|
|
local i
|
96 |
|
|
for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
|
97 |
|
|
}
|
98 |
|
|
|
99 |
9017
|
aaronmk
|
|
100 |
8854
|
aaronmk
|
#### verbose output
|
101 |
|
|
|
102 |
8973
|
aaronmk
|
# usage: (stdout2stderr; cmd...) || return
|
103 |
|
|
# `|| return` needed on Mac because of bug where -e doesn't apply to ()
|
104 |
8914
|
aaronmk
|
stdout2stderr () { exec >&2; }
|
105 |
|
|
|
106 |
9006
|
aaronmk
|
# set verbosity
|
107 |
9005
|
aaronmk
|
if isset verbose; then : "${verbosity:=$(bool2int "$verbose")}"; fi
|
108 |
9006
|
aaronmk
|
: "${verbosity=3}" # default
|
109 |
|
|
: "${verbosity:=0}" # ensure non-empty
|
110 |
9007
|
aaronmk
|
declare -i verbosity # ensure integer
|
111 |
9000
|
aaronmk
|
export verbosity # propagate the verbosity to invoked commands
|
112 |
8710
|
aaronmk
|
|
113 |
8898
|
aaronmk
|
can_log () { test "$verbosity" -gt 0; } # verbosity=0 turns off all logging
|
114 |
8895
|
aaronmk
|
|
115 |
8920
|
aaronmk
|
: "${log_indent= }"
|
116 |
8919
|
aaronmk
|
|
117 |
8900
|
aaronmk
|
# usage: in func: inc_log_level; ...
|
118 |
|
|
# outside func: inc_log_level; ...; dec_log_level
|
119 |
8919
|
aaronmk
|
alias inc_log_level='declare verbosity="$verbosity" PS4="$log_indent$PS4"
|
120 |
|
|
let verbosity--'
|
121 |
|
|
alias dec_log_level='declare verbosity="$verbosity" PS4="${PS4#$log_indent}"
|
122 |
|
|
let verbosity++'
|
123 |
8900
|
aaronmk
|
|
124 |
8995
|
aaronmk
|
fi # load new aliases
|
125 |
|
|
if self_being_included; then
|
126 |
|
|
|
127 |
8994
|
aaronmk
|
# usage: (limit_stderr; cmd...) || return
|
128 |
8973
|
aaronmk
|
# `|| return` needed on Mac because of bug where -e doesn't apply to ()
|
129 |
8995
|
aaronmk
|
limit_stderr () { inc_log_level; if ! can_log; then exec 2>/dev/null; fi; }
|
130 |
8904
|
aaronmk
|
|
131 |
8994
|
aaronmk
|
limit_stderr_cmd () # usage: [stdout2stderr=1] limit_stderr_cmd cmd...
|
132 |
8915
|
aaronmk
|
{
|
133 |
9010
|
aaronmk
|
case "$1" in echo_run) shift; echo_cmd "$@";; esac
|
134 |
8994
|
aaronmk
|
(limit_stderr
|
135 |
8915
|
aaronmk
|
if test -n "$stdout2stderr"; then stdout2stderr; fi
|
136 |
|
|
"$@"
|
137 |
8955
|
aaronmk
|
) || return
|
138 |
8915
|
aaronmk
|
}
|
139 |
9008
|
aaronmk
|
alias limit_stderr_cmd='limit_stderr_cmd ' # last space alias-expands next word
|
140 |
8911
|
aaronmk
|
|
141 |
8897
|
aaronmk
|
echo_cmd () { if can_log; then echo "$PS4$*" >&2; fi; }
|
142 |
8272
|
aaronmk
|
|
143 |
8278
|
aaronmk
|
echo_run () { echo_cmd "$@"; "$@"; }
|
144 |
|
|
|
145 |
8984
|
aaronmk
|
# auto-echo common external commands
|
146 |
|
|
for cmd in rm; do alias "$cmd=echo_run $cmd"; done; unset cmd
|
147 |
|
|
|
148 |
9011
|
aaronmk
|
# echo all external commands
|
149 |
|
|
alias extern="echo_run extern " # last space alias-expands next word
|
150 |
8907
|
aaronmk
|
|
151 |
8929
|
aaronmk
|
alias self='extern "$FUNCNAME"' # usage: wrapper () { self ...; }
|
152 |
|
|
|
153 |
8908
|
aaronmk
|
# commands that are always external
|
154 |
|
|
for cmd in env; do alias "$cmd=extern $cmd"; done; unset cmd
|
155 |
8872
|
aaronmk
|
|
156 |
8646
|
aaronmk
|
canon_rel_path ()
|
157 |
|
|
{
|
158 |
|
|
local path="$1"
|
159 |
8716
|
aaronmk
|
path="$(realpath "$path")" # canonicalize
|
160 |
8773
|
aaronmk
|
path="${path#$(pwd -P)/}" # remove any shared prefix with the current dir
|
161 |
8646
|
aaronmk
|
echo "$path"
|
162 |
|
|
}
|
163 |
|
|
|
164 |
8909
|
aaronmk
|
function echo_func ()
|
165 |
8463
|
aaronmk
|
{
|
166 |
8901
|
aaronmk
|
inc_log_level
|
167 |
8647
|
aaronmk
|
local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
|
168 |
|
|
echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
|
169 |
8463
|
aaronmk
|
}
|
170 |
8885
|
aaronmk
|
|
171 |
8997
|
aaronmk
|
# usage: cmd1 | { pipe_delay; cmd2; }
|
172 |
|
|
alias pipe_delay='sleep 0.1' # s; display after leading output of cmd1
|
173 |
|
|
|
174 |
|
|
fi # load new aliases
|
175 |
|
|
if self_being_included; then
|
176 |
|
|
|
177 |
8702
|
aaronmk
|
echo_stdin () # usage: input|echo_stdin|cmd
|
178 |
|
|
{
|
179 |
8901
|
aaronmk
|
inc_log_level
|
180 |
8897
|
aaronmk
|
if can_log; then
|
181 |
8998
|
aaronmk
|
pipe_delay
|
182 |
8897
|
aaronmk
|
echo ----- >&2
|
183 |
|
|
tee -a /dev/stderr;
|
184 |
|
|
echo ----- >&2
|
185 |
|
|
else cat
|
186 |
|
|
fi
|
187 |
8702
|
aaronmk
|
}
|
188 |
8275
|
aaronmk
|
|
189 |
8856
|
aaronmk
|
echo_vars () # usage: echo_vars var...
|
190 |
8901
|
aaronmk
|
{
|
191 |
8922
|
aaronmk
|
inc_log_level; inc_log_level
|
192 |
8921
|
aaronmk
|
if can_log; then
|
193 |
|
|
local var
|
194 |
|
|
for var in "${@%%=*}"; do { echo -n "$PS4"; declare -p "$var";} >&2;done
|
195 |
|
|
fi
|
196 |
8901
|
aaronmk
|
}
|
197 |
8641
|
aaronmk
|
|
198 |
9019
|
aaronmk
|
echo_export () { builtin export "$@"; echo_vars "$@"; }
|
199 |
8641
|
aaronmk
|
|
200 |
8713
|
aaronmk
|
if test "$verbosity" -ge 2; then
|
201 |
8711
|
aaronmk
|
alias export="echo_export" # automatically echo env vars when they are set
|
202 |
|
|
fi
|
203 |
8642
|
aaronmk
|
|
204 |
8272
|
aaronmk
|
usage () { echo "Usage: $1" >&2; (exit 2); }
|
205 |
|
|
|
206 |
8873
|
aaronmk
|
fi # load new aliases
|
207 |
|
|
if self_being_included; then
|
208 |
|
|
|
209 |
9017
|
aaronmk
|
|
210 |
8871
|
aaronmk
|
#### strings
|
211 |
|
|
|
212 |
|
|
sed_ere_flag="$(test "$(uname)" = Darwin && echo E || echo r)"
|
213 |
|
|
|
214 |
8930
|
aaronmk
|
sed () { self -"$sed_ere_flag" "$@";}
|
215 |
8871
|
aaronmk
|
|
216 |
9017
|
aaronmk
|
|
217 |
8854
|
aaronmk
|
#### vars
|
218 |
|
|
|
219 |
|
|
set_var () { eval "$1"'="$2"'; }
|
220 |
|
|
|
221 |
8857
|
aaronmk
|
set_inv () { set_var no_"$1" "$(test -n "${!1}" || echo 1)"; }
|
222 |
|
|
|
223 |
8859
|
aaronmk
|
# usage: local var=...; local_inv
|
224 |
8888
|
aaronmk
|
alias local_inv='declare "no_$var=$(test -n "${!var}" || echo 1)"'
|
225 |
8859
|
aaronmk
|
|
226 |
8863
|
aaronmk
|
# usage: local prefix=..._; import_vars
|
227 |
|
|
alias import_vars="$(cat <<'EOF'
|
228 |
|
|
: "${prefix:?}"
|
229 |
|
|
local src_var dest_var
|
230 |
|
|
for src_var in $(eval echo '${!'$prefix'*}'); do
|
231 |
|
|
dest_var="${src_var#$prefix}"
|
232 |
|
|
local "$dest_var=${!src_var}"; echo_vars "$dest_var"
|
233 |
|
|
done
|
234 |
|
|
EOF
|
235 |
|
|
)"
|
236 |
|
|
|
237 |
9017
|
aaronmk
|
|
238 |
8854
|
aaronmk
|
#### commands
|
239 |
|
|
|
240 |
8934
|
aaronmk
|
top_script="$0" # outermost script
|
241 |
|
|
top_dir="$(dirname "$top_script")"
|
242 |
8272
|
aaronmk
|
|
243 |
8986
|
aaronmk
|
# auto-removes a command's output file on error (like make's .DELETE_ON_ERROR)
|
244 |
|
|
function to_file () # usage: stdout=... to_file cmd...
|
245 |
|
|
{ : "${stdout?}"; log_e "$@" >"$stdout" || { rm "$stdout"; rethrow; }; }
|
246 |
|
|
alias to_file='to_file ' # last space alias-expands next word
|
247 |
|
|
|
248 |
8465
|
aaronmk
|
run_args_cmd () # runs the command line args command
|
249 |
8272
|
aaronmk
|
{
|
250 |
8971
|
aaronmk
|
test $? -eq 0 || return
|
251 |
8693
|
aaronmk
|
eval set -- "$(reverse "${BASH_ARGV[@]}")"
|
252 |
8971
|
aaronmk
|
test $# -ge 1 || set -- all
|
253 |
8648
|
aaronmk
|
echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
|
254 |
8272
|
aaronmk
|
}
|
255 |
|
|
|
256 |
8284
|
aaronmk
|
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
|
257 |
8272
|
aaronmk
|
{
|
258 |
8881
|
aaronmk
|
echo_func
|
259 |
8284
|
aaronmk
|
: "${subdirs?}"
|
260 |
|
|
|
261 |
8272
|
aaronmk
|
for subdir in "${subdirs[@]}"; do
|
262 |
8280
|
aaronmk
|
"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
|
263 |
8272
|
aaronmk
|
done
|
264 |
|
|
}
|
265 |
|
|
|
266 |
9017
|
aaronmk
|
|
267 |
8966
|
aaronmk
|
#### URLs
|
268 |
|
|
|
269 |
|
|
localize_url () { test _"$1" = _"$(hostname -f)" || echo "$1"; }
|
270 |
|
|
|
271 |
8704
|
aaronmk
|
fi
|