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 #### try allows command sub (bug #1608)
309 shopt --set ysh:all
310
311 try {
312 var x = $(echo hi)
313 }
314 echo $x
315
316 ## STDOUT:
317 hi
318 ## END
319
320 #### Uncaught expression error exits status 3
321 $SH -c '
322 shopt --set parse_proc
323
324 # errexit does not need to be!
325
326 var x = 42 / 0
327 echo inside=$?
328 '
329 echo outside=$?
330 ## STDOUT:
331 outside=3
332 ## END
333
334 #### boolstatus with external command
335
336 set -o errexit
337
338 echo hi > file.txt
339
340 if boolstatus grep pat file.txt; then
341 echo 'match'
342 else
343 echo 'no match'
344 fi
345
346 # file doesn't exist
347 if boolstatus grep pat BAD; then
348 echo 'match'
349 else
350 echo 'no match'
351 fi
352
353 echo DONE
354 ## status: 2
355 ## STDOUT:
356 no match
357 ## END
358
359 #### boolstatus disallows procs with strict_errexit
360 set -o errexit
361 shopt -s strict_errexit
362
363 echo hi > file.txt
364
365 not-found() {
366 echo not-found
367 grep pat file.txt
368 echo not-found
369 }
370
371 bad() {
372 echo bad
373 grep pat BAD # exits with code 2
374 echo bad
375 }
376
377 if boolstatus not-found; then
378 echo 'match'
379 else
380 echo 'no match'
381 fi
382
383 if boolstatus bad; then
384 echo 'match'
385 else
386 echo 'no match'
387 fi
388
389 ## status: 1
390 ## STDOUT:
391 ## END
392
393 #### boolstatus can call a function without strict_errexit (not recommended)
394 set -o errexit
395
396 echo hi > file.txt
397
398 not-found() {
399 echo not-found
400 grep pat file.txt
401 local status=$?
402 if test "$status" -ne 0; then
403 return $status
404 fi
405 echo not-found
406 }
407
408 bad() {
409 echo bad
410 grep pat BAD # exits with code 2
411 local status=$?
412 if test "$status" -ne 0; then
413 return $status
414 fi
415 echo bad
416 }
417
418 if boolstatus not-found; then
419 echo 'match'
420 else
421 echo 'no match'
422 fi
423
424 if boolstatus bad; then
425 echo 'match'
426 else
427 echo 'no match'
428 fi
429
430 ## status: 2
431 ## STDOUT:
432 not-found
433 no match
434 bad
435 ## END
436