1 ## oils_failures_allowed: 2
2 ## compare_shells: dash bash mksh
3
4 # Job control constructs:
5 # & terminator (instead of ;)
6 # $! -- last PID
7 # wait builtin (wait -n waits for next)
8 #
9 # Only interactive:
10 # fg
11 # bg
12 # %1 -- current job
13
14 #### wait with nothing to wait for
15 wait
16 ## status: 0
17
18 #### wait -n with nothing to wait for
19 # The 127 is STILL overloaded. Copying bash for now.
20 wait -n
21 ## status: 127
22 ## N-I dash status: 2
23 ## N-I mksh status: 1
24
25 #### wait with jobspec syntax %nonexistent
26 wait %nonexistent
27 ## status: 127
28 ## OK dash status: 2
29
30 #### wait with invalid PID
31 wait 12345678
32 ## status: 127
33
34 #### wait with invalid arg
35 wait zzz
36 ## status: 2
37 ## OK bash status: 1
38 # mksh confuses a syntax error with 'command not found'!
39 ## BUG mksh status: 127
40
41 #### Builtin in background
42 echo async &
43 wait
44 ## stdout: async
45
46 #### External command in background
47 sleep 0.01 &
48 wait
49 ## stdout-json: ""
50
51 #### Pipeline in Background
52 echo hi | { exit 99; } &
53 wait $!
54 echo status=$?
55 ## stdout: status=99
56
57 #### Wait for job doesn't support PIPESTATUS
58
59 # foreground works
60 { echo hi; exit 55; } | false
61 echo status=$? pipestatus=${PIPESTATUS[@]}
62
63 { echo hi; exit 55; } | false &
64 echo status=$? pipestatus=${PIPESTATUS[@]}
65
66 # Hm pipestatus doesn't work
67 wait %+
68 #wait %1
69 #wait $!
70 echo status=$? pipestatus=${PIPESTATUS[@]}
71
72 ## STDOUT:
73 status=1 pipestatus=55 1
74 status=0 pipestatus=0
75 status=1 pipestatus=1
76 ## END
77 ## N-I dash status: 2
78 ## N-I dash stdout-json: ""
79
80 #### Brace group in background, wait all
81 { sleep 0.09; exit 9; } &
82 { sleep 0.07; exit 7; } &
83 wait # wait for all gives 0
84 echo "status=$?"
85 ## stdout: status=0
86
87 #### Wait on background process PID
88 { sleep 0.09; exit 9; } &
89 pid1=$!
90 { sleep 0.07; exit 7; } &
91 pid2=$!
92 wait $pid2
93 echo "status=$?"
94 wait $pid1
95 echo "status=$?"
96 ## stdout-json: "status=7\nstatus=9\n"
97
98 #### Wait on multiple specific IDs returns last status
99 { sleep 0.08; exit 8; } &
100 jid1=$!
101 { sleep 0.09; exit 9; } &
102 jid2=$!
103 { sleep 0.07; exit 7; } &
104 jid3=$!
105 wait $jid1 $jid2 $jid3 # NOTE: not using %1 %2 %3 syntax on purpose
106 echo "status=$?" # third job I think
107 ## stdout: status=7
108
109 #### wait -n
110 case $SH in (dash|mksh) return ;; esac
111
112 { sleep 0.09; exit 9; } &
113 { sleep 0.03; exit 3; } &
114 wait -n
115 echo "status=$?"
116 wait -n
117 echo "status=$?"
118 ## STDOUT:
119 status=3
120 status=9
121 ## END
122 ## N-I dash/mksh stdout-json: ""
123
124 #### Async for loop
125 for i in 1 2 3; do
126 echo $i
127 sleep 0.0$i
128 done &
129 wait
130 ## stdout-json: "1\n2\n3\n"
131 ## status: 0
132
133 #### Background process doesn't affect parent
134 echo ${foo=1}
135 echo $foo
136 echo ${bar=2} &
137 wait
138 echo $bar # bar is NOT SET in the parent process
139 ## stdout-json: "1\n1\n2\n\n"
140
141 #### Background process and then a singleton pipeline
142
143 # This was inspired by #416, although that symptom there was timing, so it's
144 # technically not a regression test. It's hard to test timing.
145
146 { sleep 0.1; exit 42; } &
147 echo begin
148 ! true
149 echo end
150 wait $!
151 echo status=$?
152 ## STDOUT:
153 begin
154 end
155 status=42
156 ## END
157
158 #### jobs prints one line per job
159 sleep 0.1 &
160 sleep 0.1 | cat &
161
162 # dash doesn't print if it's not a terminal?
163 jobs | wc -l
164
165 ## STDOUT:
166 2
167 ## END
168 ## BUG dash STDOUT:
169 0
170 ## END
171
172 #### jobs -p prints one line per job
173 sleep 0.1 &
174 sleep 0.1 | cat &
175
176 jobs -p > tmp.txt
177
178 cat tmp.txt | wc -l # 2 lines, one for each job
179 cat tmp.txt | wc -w # each line is a single "word"
180
181 ## STDOUT:
182 2
183 2
184 ## END