1 #### try usage error
2
3 # Irony: we can't fail that hard here because errexit is disabled before
4 # we enable it.
5 # TODO: We could special case this perhaps
6
7 try
8 echo status=$?
9
10 try -z
11 echo status=$?
12
13 try --zoo
14 echo status=$?
15
16 # -- is allowed
17 try -- echo hi
18
19 ## STDOUT:
20 status=2
21 status=2
22 status=2
23 hi
24 ## END
25
26 #### try sets _status
27 myproc() {
28 echo 'myproc'
29 return 42
30 }
31
32 try myproc
33 echo dollar=$?
34 echo _status=$_status
35
36 ( exit 9 )
37 echo dollar=$?
38 echo _status=$_status
39
40 ## STDOUT:
41 myproc
42 dollar=0
43 _status=42
44 dollar=9
45 _status=42
46 ## END
47
48
49 #### try with and without errexit
50 shopt --set parse_brace parse_proc
51
52 myproc() {
53 echo before
54 false
55 echo after
56 }
57
58 try myproc
59 echo status=$_status
60
61 echo ---
62 try {
63 echo 'block'
64 myproc
65 }
66 echo status=$_status
67
68 echo ===
69 set -o errexit
70
71 try myproc
72 echo status=$_status
73
74 echo ---
75 try {
76 echo 'block'
77 myproc
78 }
79 echo status=$_status
80
81 ## STDOUT:
82 before
83 status=1
84 ---
85 block
86 before
87 status=1
88 ===
89 before
90 status=1
91 ---
92 block
93 before
94 status=1
95 ## END
96
97 #### try takes a block
98 shopt --set parse_brace
99
100 myproc() {
101 echo 'myproc'
102 return 42
103 }
104
105 try {
106 myproc
107 }
108 echo dollar=$?
109 echo _status=$_status
110
111 # It works the same with errexit
112 set -o errexit
113
114 try {
115 myproc
116 }
117 echo dollar=$?
118 echo _status=$_status
119
120 ## STDOUT:
121 myproc
122 dollar=0
123 _status=42
124 myproc
125 dollar=0
126 _status=42
127 ## END
128
129 #### try with _pipeline_status and PIPESTATUS
130 shopt --set parse_brace parse_at
131 set -o errexit
132
133 try {
134 ls /bad | wc -l
135 }
136 echo p @_pipeline_status
137 echo p @PIPESTATUS
138 echo _status=$_status # 0 because pipefail is off
139
140 echo ---
141 set -o pipefail
142 try {
143 ls /bad | wc -l
144 }
145 echo p @_pipeline_status
146 echo p @PIPESTATUS
147 echo _status=$_status # changed to 2 because of pipefail
148
149 ## STDOUT:
150 0
151 p 2 0
152 p 2 0
153 _status=0
154 ---
155 0
156 p 2 0
157 p 2 0
158 _status=2
159 ## END
160
161 #### try with _process_sub_status
162 shopt --set parse_brace parse_at
163 set -o errexit
164
165 touch right.txt
166
167 try {
168 diff -q <(sort OOPS) <(sort right.txt)
169 }
170 echo p @_process_sub_status
171 echo _status=$_status
172
173 echo ---
174 shopt --set process_sub_fail
175 try {
176 diff -q <(sort OOPS) <(sort right.txt)
177 }
178 echo p @_process_sub_status
179 echo _status=$_status # changed to 2 because of process_sub_fail
180
181 ## STDOUT:
182 p 2 0
183 _status=0
184 ---
185 p 2 0
186 _status=2
187 ## END
188
189 #### try error handling idioms
190 shopt --set parse_paren parse_brace parse_at
191
192 myproc() {
193 return 42
194 }
195
196 try myproc
197 if (_status === 0) {
198 echo 'OK'
199 }
200
201 try myproc
202 if (_status !== 0) {
203 echo 'fail'
204 }
205
206 try {
207 ls /nonexistent | wc -l
208 }
209 # make sure it's integer comparison
210 if (_pipeline_status[0] !== 0) {
211 echo 'pipeline failed:' @_pipeline_status
212 }
213
214 try {
215 diff <(sort XX) <(sort YY)
216 }
217 # make sure it's integer comparison
218 if (_process_sub_status[0] !== 0) {
219 echo 'process sub failed:' @_process_sub_status
220 }
221
222 ## STDOUT:
223 fail
224 0
225 pipeline failed: 2 0
226 process sub failed: 2 2
227 ## END
228
229 #### try can handled failed var, setvar, etc.
230 shopt --set parse_brace parse_proc
231
232 try {
233 echo hi
234 var x = 1 / 0
235 echo 'should not get here'
236 }
237 echo div $_status
238
239 try {
240 var a = []
241 setvar item = a[1]
242 echo 'should not get here'
243 }
244 echo index $_status
245
246 try {
247 var d = {}
248 setvar item = d['mykey']
249 echo 'should not get here'
250 }
251 echo key $_status
252
253 try {
254 setvar item = d->mykey
255 echo 'should not get here'
256 }
257 echo arrow $_status
258
259 ## STDOUT:
260 hi
261 div 3
262 index 3
263 key 3
264 arrow 3
265 ## END
266
267 # nothing on stderr because it's caught!
268
269 ## STDERR:
270 ## END
271
272 #### try can handled failed expr sub
273 shopt --set parse_brace parse_proc
274
275 try {
276 echo hi
277
278 var d = {}
279 echo "result = $[d->BAD]"
280 echo 'should not get here'
281 }
282 echo _status=$_status
283 ## STDOUT:
284 hi
285 _status=3
286 ## END
287 ## STDERR:
288 ## END
289
290 #### try with failed command sub within expression
291 shopt --set parse_brace parse_proc
292
293 try {
294 echo hi
295 var x = $(exit 42) # errexit
296 echo bye
297 }
298 echo try $_status
299
300 # Note that there's no way to retrieve this status WITHOUT try
301 # var x = $(exit 42) # errexit
302
303 ## STDOUT:
304 hi
305 try 42
306 ## END
307
308 #### Uncaught expression error exits status 3
309 $SH -c '
310 shopt --set parse_proc
311
312 # errexit does not need to be!
313
314 var x = 42 / 0
315 echo inside=$?
316 '
317 echo outside=$?
318 ## STDOUT:
319 outside=3
320 ## END
321
322 #### boolstatus with external command
323
324 set -o errexit
325
326 echo hi > file.txt
327
328 if boolstatus grep pat file.txt; then
329 echo 'match'
330 else
331 echo 'no match'
332 fi
333
334 # file doesn't exist
335 if boolstatus grep pat BAD; then
336 echo 'match'
337 else
338 echo 'no match'
339 fi
340
341 echo DONE
342 ## status: 2
343 ## STDOUT:
344 no match
345 ## END
346
347 #### boolstatus disallows procs with strict_errexit
348 set -o errexit
349 shopt -s strict_errexit
350
351 echo hi > file.txt
352
353 not-found() {
354 echo not-found
355 grep pat file.txt
356 echo not-found
357 }
358
359 bad() {
360 echo bad
361 grep pat BAD # exits with code 2
362 echo bad
363 }
364
365 if boolstatus not-found; then
366 echo 'match'
367 else
368 echo 'no match'
369 fi
370
371 if boolstatus bad; then
372 echo 'match'
373 else
374 echo 'no match'
375 fi
376
377 ## status: 1
378 ## STDOUT:
379 ## END
380
381 #### boolstatus can call a function without strict_errexit (not recommended)
382 set -o errexit
383
384 echo hi > file.txt
385
386 not-found() {
387 echo not-found
388 grep pat file.txt
389 local status=$?
390 if test "$status" -ne 0; then
391 return $status
392 fi
393 echo not-found
394 }
395
396 bad() {
397 echo bad
398 grep pat BAD # exits with code 2
399 local status=$?
400 if test "$status" -ne 0; then
401 return $status
402 fi
403 echo bad
404 }
405
406 if boolstatus not-found; then
407 echo 'match'
408 else
409 echo 'no match'
410 fi
411
412 if boolstatus bad; then
413 echo 'match'
414 else
415 echo 'no match'
416 fi
417
418 ## status: 2
419 ## STDOUT:
420 not-found
421 no match
422 bad
423 ## END
424