1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Usage:
|
4 | # ./background-status.sh <function name>
|
5 |
|
6 | set -o nounset
|
7 | #set -o pipefail
|
8 | #set -o errexit # wait waill fail with this
|
9 |
|
10 | do_some_work() {
|
11 | sleep 0.2
|
12 | exit $1
|
13 | }
|
14 |
|
15 | wait_pids() {
|
16 | { sleep 0.1; exit 5; } &
|
17 | pid1=$!
|
18 |
|
19 | do_some_work 6 &
|
20 | pid2=$!
|
21 |
|
22 | { sleep 0.3; exit 7; } &
|
23 | pid3=$!
|
24 |
|
25 | do_some_work 8 &
|
26 | pid4=$!
|
27 |
|
28 | echo "Waiting for PIDs $pid1 $pid2 $pid3"
|
29 |
|
30 | wait $pid1; echo $?
|
31 | wait $pid2; echo $?
|
32 | wait $pid3; echo $?
|
33 | wait $pid4; echo $?
|
34 |
|
35 | echo 'Done'
|
36 | }
|
37 |
|
38 | # NOTE: dash/mksh/zsh all lack the -n option. That seems like an oversight.
|
39 | wait_next() {
|
40 | { sleep 0.1; exit 8; } &
|
41 | pid1=$!
|
42 |
|
43 | { sleep 0.2; exit 9; } &
|
44 | pid2=$!
|
45 |
|
46 | { sleep 0.3; exit 10; } &
|
47 | pid3=$!
|
48 |
|
49 | wait -n; echo $?
|
50 | wait -n; echo $?
|
51 | wait -n; echo $?
|
52 |
|
53 | echo 'Done'
|
54 | }
|
55 |
|
56 | both() {
|
57 | wait_pids
|
58 | wait_next
|
59 | }
|
60 |
|
61 | # ERROR: says "no job control"
|
62 | job_control() {
|
63 | { sleep 0.1; exit 8; } &
|
64 | fg
|
65 | }
|
66 |
|
67 | # ERROR: says "no job control"
|
68 | job_control2() {
|
69 | { sleep 0.1; exit 8; } &
|
70 | %1
|
71 | }
|
72 |
|
73 | "$@"
|