1 # xtrace test. Test PS4 and line numbers, etc.
2
3 ## oils_failures_allowed: 1
4 ## compare_shells: bash dash mksh
5
6 #### unset PS4
7 case $SH in dash) echo 'weird bug'; exit ;; esac
8
9 set -x
10 echo 1
11 unset PS4
12 echo 2
13 ## STDOUT:
14 1
15 2
16 ## STDERR:
17 + echo 1
18 + unset PS4
19 echo 2
20 ## END
21
22 ## BUG dash STDOUT:
23 weird bug
24 ## END
25 ## BUG dash STDERR:
26 ## END
27
28 #### set -o verbose prints unevaluated code
29 set -o verbose
30 x=foo
31 y=bar
32 echo $x
33 echo $(echo $y)
34 ## STDOUT:
35 foo
36 bar
37 ## STDERR:
38 x=foo
39 y=bar
40 echo $x
41 echo $(echo $y)
42 ## OK bash STDERR:
43 x=foo
44 y=bar
45 echo $x
46 echo $(echo $y)
47 ## END
48
49 #### xtrace with unprintable chars
50 case $SH in (dash) exit ;; esac
51
52 s=$'a\x03b\004c\x00d'
53 set -o xtrace
54 echo "$s"
55 ## stdout-repr: 'a\x03b\x04c\x00d\n'
56 ## STDERR:
57 + echo $'a\u0003b\u0004c\u0000d'
58 ## END
59 ## OK bash stdout-repr: 'a\x03b\x04c\n'
60 ## OK bash stderr-repr: "+ echo $'a\\003b\\004c'\n"
61
62 # nonsensical output?
63 ## BUG mksh stdout-repr: 'a;\x04c\r\n'
64 ## BUG mksh stderr-repr: "+ echo $'a;\\004c\\r'\n"
65 ## N-I dash stdout-json: ""
66 ## N-I dash stderr-json: ""
67
68 #### xtrace with unicode chars
69 case $SH in (dash) exit ;; esac
70
71 mu1='[μ]'
72 mu2=$'[\u03bc]'
73
74 set -o xtrace
75 echo "$mu1" "$mu2"
76
77 ## STDOUT:
78 [μ] [μ]
79 ## END
80 ## STDERR:
81 + echo '[μ]' '[μ]'
82 ## END
83 ## N-I dash stdout-json: ""
84 ## N-I dash stderr-json: ""
85
86 #### xtrace with paths
87 set -o xtrace
88 echo my-dir/my_file.cc
89 ## STDOUT:
90 my-dir/my_file.cc
91 ## END
92 ## STDERR:
93 + echo my-dir/my_file.cc
94 ## END
95
96 #### xtrace with tabs
97 case $SH in (dash) exit ;; esac
98
99 set -o xtrace
100 echo $'[\t]'
101 ## stdout-json: "[\t]\n"
102 ## STDERR:
103 + echo $'[\t]'
104 ## END
105 # this is a bug because it's hard to see
106 ## BUG bash stderr-json: "+ echo '[\t]'\n"
107 ## N-I dash stdout-json: ""
108 ## N-I dash stderr-json: ""
109
110 #### xtrace with whitespace, quotes, and backslash
111 set -o xtrace
112 echo '1 2' \' \" \\
113 ## STDOUT:
114 1 2 ' " \
115 ## END
116
117 # YSH is different because backslashes require $'\\' and not '\', but that's OK
118 ## STDERR:
119 + echo '1 2' $'\'' '"' $'\\'
120 ## END
121
122 ## OK bash/mksh STDERR:
123 + echo '1 2' \' '"' '\'
124 ## END
125
126 ## BUG dash STDERR:
127 + echo 1 2 ' " \
128 ## END
129
130 #### xtrace with newlines
131 # bash and dash trace this badly. They print literal newlines, which I don't
132 # want.
133 set -x
134 echo $'[\n]'
135 ## STDOUT:
136 [
137 ]
138 ## STDERR:
139 + echo $'[\n]'
140 ## END
141 # bash has ugly output that spans lines
142 ## OK bash STDERR:
143 + echo '[
144 ]'
145 ## END
146 ## N-I dash stdout-json: "$[\n]\n"
147 ## N-I dash stderr-json: "+ echo $[\\n]\n"
148
149 #### xtrace written before command executes
150 set -x
151 echo one >&2
152 echo two >&2
153 ## stdout-json: ""
154 ## STDERR:
155 + echo one
156 one
157 + echo two
158 two
159 ## OK mksh STDERR:
160 # mksh traces redirects!
161 + >&2
162 + echo one
163 one
164 + >&2
165 + echo two
166 two
167 ## END
168
169 #### Assignments and assign builtins
170 set -x
171 x=1 x=2; echo $x; readonly x=3
172 ## STDOUT:
173 2
174 ## END
175 ## STDERR:
176 + x=1
177 + x=2
178 + echo 2
179 + readonly x=3
180 ## END
181 ## OK dash STDERR:
182 + x=1 x=2
183 + echo 2
184 + readonly x=3
185 ## END
186 ## OK dash STDERR:
187 + x=1 x=2
188 + echo 2
189 + readonly x=3
190 ## END
191 ## OK bash STDERR:
192 + x=1
193 + x=2
194 + echo 2
195 + readonly x=3
196 + x=3
197 ## END
198 ## OK mksh STDERR:
199 + x=1 x=2
200 + echo 2
201 + readonly 'x=3'
202 ## END
203
204 #### [[ ]]
205 case $SH in (dash|mksh) exit ;; esac
206
207 set -x
208
209 dir=/
210 if [[ -d $dir ]]; then
211 (( a = 42 ))
212 fi
213 ## stdout-json: ""
214 ## STDERR:
215 + dir=/
216 + [[ -d $dir ]]
217 + (( a = 42 ))
218 ## END
219 ## OK bash STDERR:
220 + dir=/
221 + [[ -d / ]]
222 + (( a = 42 ))
223 ## END
224 ## N-I dash/mksh stderr-json: ""
225
226 #### PS4 is scoped
227 set -x
228 echo one
229 f() {
230 local PS4='- '
231 echo func;
232 }
233 f
234 echo two
235 ## STDERR:
236 + echo one
237 + f
238 + local 'PS4=- '
239 - echo func
240 + echo two
241 ## END
242 ## OK osh STDERR:
243 + echo one
244 + f
245 + local PS4='- '
246 - echo func
247 + echo two
248 ## END
249 ## OK dash STDERR:
250 # dash loses information about spaces! There is a trailing space, but you
251 # can't see it.
252 + echo one
253 + f
254 + local PS4=-
255 - echo func
256 + echo two
257 ## END
258 ## OK mksh STDERR:
259 # local gets turned into typeset
260 + echo one
261 + f
262 + typeset 'PS4=- '
263 - echo func
264 + echo two
265 ## END
266
267 #### xtrace with variables in PS4
268 PS4='+$x:'
269 set -o xtrace
270 x=1
271 echo one
272 x=2
273 echo two
274 ## STDOUT:
275 one
276 two
277 ## END
278
279 ## STDERR:
280 +:x=1
281 +1:echo one
282 +1:x=2
283 +2:echo two
284 ## END
285
286 ## OK mksh STDERR:
287 # mksh has trailing spaces
288 +:x=1
289 +1:echo one
290 +1:x=2
291 +2:echo two
292 ## END
293
294 ## OK osh/dash STDERR:
295 # the PS4 string is evaluated AFTER the variable is set. That's OK
296 +1:x=1
297 +1:echo one
298 +2:x=2
299 +2:echo two
300 ## END
301
302 #### PS4 with unterminated ${
303 # osh shows inline error; maybe fail like dash/mksh?
304 x=1
305 PS4='+${x'
306 set -o xtrace
307 echo one
308 echo status=$?
309 ## STDOUT:
310 one
311 status=0
312 ## END
313 # mksh and dash both fail. bash prints errors to stderr.
314 ## OK dash stdout-json: ""
315 ## OK dash status: 2
316 ## OK mksh stdout-json: ""
317 ## OK mksh status: 1
318
319 #### PS4 with unterminated $(
320 # osh shows inline error; maybe fail like dash/mksh?
321 x=1
322 PS4='+$(x'
323 set -o xtrace
324 echo one
325 echo status=$?
326 ## STDOUT:
327 one
328 status=0
329 ## END
330 # mksh and dash both fail. bash prints errors to stderr.
331 ## OK dash stdout-json: ""
332 ## OK dash status: 2
333 ## OK mksh stdout-json: ""
334 ## OK mksh status: 1
335
336 #### PS4 with runtime error
337 # osh shows inline error; maybe fail like dash/mksh?
338 x=1
339 PS4='+oops $(( 1 / 0 )) \$'
340 set -o xtrace
341 echo one
342 echo status=$?
343 ## STDOUT:
344 one
345 status=0
346 ## END
347 # mksh and dash both fail. bash prints errors to stderr.
348 ## OK dash stdout-json: ""
349 ## OK dash status: 2
350 ## OK mksh stdout-json: ""
351 ## OK mksh status: 1
352
353
354 #### Reading $? in PS4
355 PS4='[last=$?] '
356 set -x
357 false
358 echo ok
359 ## STDOUT:
360 ok
361 ## END
362 ## STDERR:
363 [last=0] false
364 [last=1] echo ok
365 ## END
366 ## OK osh STDERR:
367 [last=0] 'false'
368 [last=1] echo ok
369 ## END