OILS / spec / redirect-multi.test.sh View on Github | oilshell.org

308 lines, 121 significant
1## compare_shells: bash mksh zsh ash
2
3#### File redirects with glob args (bash and zsh only)
4
5touch one-bar
6
7echo hi > one-*
8
9cat one-bar
10
11echo escaped > one-\*
12
13cat one-\*
14
15## STDOUT:
16hi
17escaped
18## END
19## N-I dash/mksh/ash STDOUT:
20escaped
21## END
22
23#### File redirect without matching any file
24
25echo hi > zz-*-xx
26echo status=$?
27
28echo zz*
29
30## STDOUT:
31status=0
32zz-*-xx
33## END
34
35## OK zsh status: 1
36## OK zsh STDOUT:
37## END
38
39#### ysh behavior when glob doesn't match
40
41shopt -s ysh:upgrade
42
43echo hi > qq-*-zz
44echo status=$?
45
46echo qq*
47
48## status: 1
49## STDOUT:
50## END
51
52## N-I bash/mksh/ash status: 0
53## N-I bash/mksh/ash STDOUT:
54status=0
55qq-*-zz
56## END
57
58#### File redirect without matching any file, with failglob
59
60shopt -s failglob
61
62echo hi > zz-*-xx
63echo status=$?
64
65echo zz*
66echo status=$?
67
68## STDOUT:
69status=1
70status=1
71## END
72## N-I mksh/ash STDOUT:
73status=0
74zz-*-xx
75status=0
76## END
77
78## OK zsh status: 1
79## OK zsh STDOUT:
80## END
81
82#### Redirect to $empty (in function body)
83empty=''
84fun() { echo hi; } > $empty
85fun
86echo status=$?
87## STDOUT:
88status=1
89## END
90## OK dash STDOUT:
91status=2
92## END
93
94#### Redirect to ''
95echo hi > ''
96echo status=$?
97## STDOUT:
98status=1
99## END
100
101
102
103#### File redirect to $var with glob char
104
105touch two-bar
106
107star='*'
108
109# This gets glob-expanded, as it does outside redirects
110echo hi > two-$star
111echo status=$?
112
113head two-bar two-\*
114
115## status: 1
116## STDOUT:
117status=0
118==> two-bar <==
119hi
120## END
121
122## OK mksh/zsh/ash status: 0
123## OK mksh/zsh/ash STDOUT:
124status=0
125==> two-bar <==
126
127==> two-* <==
128hi
129## END
130
131
132#### File redirect that globs to more than one file (bash and zsh only)
133
134touch foo-bar
135touch foo-spam
136
137echo hi > foo-*
138echo status=$?
139
140head foo-bar foo-spam
141
142## STDOUT:
143status=1
144==> foo-bar <==
145
146==> foo-spam <==
147## END
148
149## N-I dash/mksh/ash STDOUT:
150status=0
151==> foo-bar <==
152
153==> foo-spam <==
154## END
155
156## BUG zsh STDOUT:
157status=0
158==> foo-bar <==
159hi
160
161==> foo-spam <==
162hi
163## END
164
165#### File redirect with extended glob
166
167shopt -s extglob
168
169touch foo-bar
170
171echo hi > @(*-bar|other)
172echo status=$?
173
174cat foo-bar
175
176## status: 0
177## STDOUT:
178status=0
179hi
180## END
181
182## N-I zsh status: 1
183## N-I dash/ash status: 2
184
185## N-I dash/zsh/ash STDOUT:
186## END
187
188## BUG mksh status: 0
189## BUG mksh STDOUT:
190status=0
191## END
192
193#### Extended glob that doesn't match anything
194shopt -s extglob
195rm bad_*
196
197# They actually write this literal file! This is what EvalWordToString() does,
198# as opposed to _EvalWordToParts.
199echo foo > bad_@(*.cc|*.h)
200echo status=$?
201
202echo bad_*
203
204shopt -s failglob
205
206# Note: ysh:ugprade doesn't allow extended globs
207# shopt -s ysh:upgrade
208
209echo foo > bad_@(*.cc|*.h)
210echo status=$?
211
212## STDOUT:
213status=0
214bad_@(*.cc|*.h)
215status=1
216## END
217## N-I mksh STDOUT:
218status=0
219bad_@(*.cc|*.h)
220status=0
221## END
222
223## N-I ash status: 2
224## N-I ash stdout-json: ""
225
226## N-I zsh status: 1
227## N-I zsh stdout-json: ""
228
229
230#### Non-file redirects don't respect glob args (we differe from bash)
231
232touch 10
233
234exec 10>&1 # open stdout as descriptor 10
235
236# Does this go to stdout? ONLY bash respects it, not zsh
237echo should-not-be-on-stdout >& 1*
238
239echo stdout
240echo stderr >&2
241
242## status: 0
243
244## STDOUT:
245stdout
246## END
247
248## BUG bash STDOUT:
249should-not-be-on-stdout
250stdout
251## END
252
253## N-I dash/zsh status: 127
254## N-I dash/zsh STDOUT:
255## END
256
257
258#### Redirect with brace expansion isn't allowed
259
260echo hi > a-{one,two}
261echo status=$?
262
263head a-*
264echo status=$?
265
266
267## STDOUT:
268status=1
269status=1
270## END
271
272## N-I mksh/ash STDOUT:
273status=0
274hi
275status=0
276## END
277
278## BUG zsh STDOUT:
279status=0
280==> a-one <==
281hi
282
283==> a-two <==
284hi
285status=0
286## END
287
288
289#### File redirects have word splitting too!
290
291file='foo bar'
292
293echo hi > $file
294echo status=$?
295
296cat "$file"
297echo status=$?
298
299## STDOUT:
300status=1
301status=1
302## END
303
304## OK mksh/zsh/ash STDOUT:
305status=0
306hi
307status=0
308## END