-
Notifications
You must be signed in to change notification settings - Fork 53
/
func_waitFor
executable file
·55 lines (43 loc) · 1.28 KB
/
func_waitFor
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
#!/bin/bash
# Print a message while awaiting completion of a command. Command's output is stored in $waitFor_output
# by Todd Stein
# $1 is the command you want to run
# $2 is the message you want displayed while waiting for $1 to complete
# Example:
# waitFor "sleep 5" "Sleeping for 5 seconds..."
waitFor_SHM_FILE="/dev/shm/waitFor_output.$$"
waitFor_ANIMATION="███▓▒░ ░▒▓"
waitFor_runChild() {
$1 &>"$waitFor_SHM_FILE"
kill -s SIGUSR1 $$
}
waitFor_childDone() { # on SIGHUP
trap - SIGUSR1
break 2
}
waitFor_untilDone() { # display message until SIGHUP
trap waitFor_childDone SIGUSR1
local i
printf "\033[?25l%s " "$@" 1>&2 # hide cursor and print message
while :; do
for ((i=0; i<${#waitFor_ANIMATION}; i++)); do
local graphic=${waitFor_ANIMATION:$i:1}
printf "\b%s" "$graphic" 1>&2
sleep .1
done
done
}
waitFor_end() {
ps -o ppid,pid $waitFor_child_pid | awk -v parent=$$ '$1==parent { print $2 }' | xargs -r kill 2>/dev/null
printf '\r\033[K\033[?25h' 1>&2 # returns cursor to far left, clears the current row, and makes the cursor visible again
waitFor_output=$(<"$waitFor_SHM_FILE")
rm -f "$waitFor_SHM_FILE"
trap - EXIT
}
waitFor() {
trap waitFor_end EXIT
waitFor_runChild "$1" &
waitFor_child_pid=$!
waitFor_untilDone "$2"
waitFor_end
}