-
Notifications
You must be signed in to change notification settings - Fork 1
/
bash.sh
executable file
·391 lines (320 loc) · 7.2 KB
/
bash.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/bin/bash
#
# A collection of bash snippets and functions.
#
##########
# Arrays #
##########
#
# ### Array contains
#
# The only proper way to do this is by looping through each item. Other
# solutions claim to do this using string matching, but they are often
# dangerous because don't handle edge cases.
#
#
# Returns 0 if item is in the array; 1 otherwise.
#
# $1: The array value to search for
# $@: The array values, e.g. "${myarray[@]}"
#
array_contains() {
local item=$1; shift
for val in "$@"; do
if [ "$val" == "$item" ]; then
return 0
fi
done
return 1
}
# ### Array filter
#
# Return all elements of an array with the specified item removed.
#
# $1: The array value to remove
# $@: The array values, e.g. "${myarray[@]}"
#
array_filter() {
local item=$1; shift
for val in "$@"; do
if [ "$val" != "$item" ]; then
echo $val
fi
done
}
# ### Array join
#
# Join array elements with a string.
#
# $1: String separator
# $@: Array elements
#
array_join() {
local sep=$1; shift
IFS=$sep eval 'echo "$*"'
}
################
# Benchmarking #
################
# ### Time a command
#
# Time the number of seconds it takes to run a command; store the value in a
# variable with the specified name.
#
timer() {
local var_name=$1; shift
local end
local -i ret=0
local start
local total
start=$(date +%s)
"$@" || ret=$?
end=$(date +%s)
total=$((end - start))
# Store the value
eval "${var_name}=${total}"
return $ret
}
#################
# Path handling #
#################
#
# ### Resolve absolute path
#
# There are several snippets of code on the internet to resolve the absolute
# path, but most of them have issues. This function works on files, directories
# and handles "." and "..".
#
#
# Print the absolute path of the target path. Note that the path must be a
# real path to a file or directory or else this will fail.
#
# $1: the target file or directory
#
absolute_path() {
local path=$1
if [ "$path" == "." ]; then
echo "$(pwd)"
elif [ "$path" == ".." ]; then
echo "$(dirname "$(pwd)")"
else
path="$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
# Replace double slashes, happens when we're operating at the root
echo "${path/\/\///}"
fi
}
#
# ### Recursively resolve symlinks
#
# This does what `realpath` from coreutils does but works without it. Requires
# readlink instead.
#
#
# Recursively resolve all symlinks at the specified path and then print the
# final, absolute path.
#
# $1: the path to resolve
#
resolve_symlinks() {
(
local path=$1
while [ -L "$path" ]; do
dir="$(dirname "$(absolute_path "$path")")"
cd "$dir"
path=$(readlink "$path")
done
echo "$(absolute_path "$path")"
)
}
####################
# Process handling #
####################
# ### Clean up child processes on exit
#
# ```
# trap "exit" INT TERM
# trap "kill 0" EXIT
# ```
#
#
# Notes:
#
# * Doing the kill on exit means children will be cleaned up even during normal
# exit too.
# * Binding INT/TERM to `exit` avoids an infinite loop in signal handling.
# * `kill 0` (with `0` as the PID) sends a TERM to the entire process group.
#
# ### Get PID of a subshell
#
# **Bash3:**
#
# ```
# (sh -c 'echo $PPID' && :)
# ```
#
# Notes:
#
# * Two statements are required to actually create the subshell
# * The second statement is essentially a "no-op"
#
# **Bash4:**
#
# ```
# echo $BASHPID
# ```
# ### Run commands in parallel
#
# This provides the ability to run commands in parallel with a predefined
# number of threads.
#
# People tend to use GNU `parallel` for this, however, implementing this as a
# shell function has the following advantages:
#
# * It is portable (doesn't require `parallel` to be installed).
# * You can run shell functions as commands, whereas external programs require
# the commands to be standalone binaries.
#
# Run commands in parallel.
#
parallel() {
local max_threads=$1
local -a pids=()
local ret=0
# Set up named pipe to communicate between procs
fifo="$(mktemp)" && rm -f "$fifo"
mkfifo -m 0700 "$fifo"
# Open pipe as fd 3
exec 3<>$fifo
rm -f $fifo # Clean up pipe from filesystem; it stays open, however
local running=0
while read cmd; do
# Block, when at max_threads
while ((running >= max_threads)); do
if read -u 3 cpid ; then
wait $cpid || true
((--running))
fi
done
# Spawn child proc
($cmd; sh -c 'echo $PPID 1>&3' && :) &
pids+=($!)
((++running))
done
# Return 1 if one or more pids returned a nonzero code
for pid in "${pids[@]}"; do
wait "$pid" || ret=1
done
return $ret
}
#
# Notes:
#
# * Provide the commands as an input to this function
#
# ### Wait for background processes to exit
#
# ```
# wait [PID [...]]
# ```
#
# Notes:
#
# * If you use `wait` without specifying a PID, bash will wait for *all*
# background processes.
# * If you want to catch the return code of the background process, you
# **must** specify the PID.
# * If you specify multiple PIDs, the return code of `wait` will be the return
# code of the *last* PID specified.
#
###########
# Testing #
###########
# ### List all test functions
#
# Outputs the name of all functions starting with `test_`.
#
# ```
# $(compgen -A function | grep -E ^test_)
# ```
# ### Run test functions
#
# Runs the specified test functions and outputs the result. If a test fails,
# it outputs a trace of the test to stderr.
#
#
# Run specified tests.
#
tests() {
local ret=0
local test_log="/tmp/${0##*/}.test.log"
for test in "$@"; do
:>"$test_log"
echo -n "$test: "
if (set -x; $test) 2>"$test_log"; then
echo "OK"
else
echo "FAILED"
ret=1
{
echo
echo "Trace:"
echo -e "\033[33m"
cat "$test_log"
echo -e "\033[0m"
} >&2
fi
done
return $ret
}
########
# Time #
########
# ### Human-readable time output
#
# Given seconds, outputs something like "2m 5s".
#
#
# Output human readable duration, given seconds as an input.
#
prettify_time() {
local time_secs=$1
local hours
local mins
local secs
local -a buf=()
hours=$(($time_secs / 3600))
mins=$((($time_secs % 3600) / 60))
secs=$(($time_secs % 60))
if [ $secs -eq 0 ] && [ $mins -eq 0 ] && [ $hours -eq 0 ]; then
echo "0s"
return
fi
if [ $hours -gt 0 ]; then
buf+=("${hours}h")
fi
if [ $mins -gt 0 ]; then
buf+=("${mins}m")
fi
if [ $secs -gt 0 ]; then
buf+=("${secs}s")
fi
# Join buffer array elemts with space
IFS=" " eval 'echo "${buf[*]}"'
}
#############
# Utilities #
#############
# ### Check a required program is installed
#
# $@: list of programs to check for
#
require() {
local ret=0
for bin in "$@"; do
if ! which "$bin" &>/dev/null; then
echo "ERROR: Missing required dependency: ${bin}" >&2
ret=1
fi
done
return $ret
}