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

306 lines, 155 significant
1## oils_failures_allowed: 0
2## compare_shells: bash dash mksh
3
4# Notes:
5# - ash is just like dash, so don't bother testing
6# - zsh fails several cases
7
8#### >$file touches a file
9rm -f myfile
10test -f myfile
11echo status=$?
12
13>myfile
14test -f myfile
15echo status=$?
16
17## STDOUT:
18status=1
19status=0
20## END
21
22# regression for OSH
23## stderr-json: ""
24
25#### $(< $file) yields the contents of the file
26
27echo FOO > myfile
28foo=$(< myfile)
29echo $foo
30
31## STDOUT:
32FOO
33## END
34
35## N-I dash/ash/yash STDOUT:
36
37## END
38
39#### $(< file) with more statements
40
41# note that it doesn't do this without a command sub!
42# It's apparently a special case in bash, mksh, and zsh?
43foo=$(echo begin; < myfile)
44echo $foo
45echo ---
46
47foo=$(< myfile; echo end)
48echo $foo
49echo ---
50
51foo=$(< myfile; <myfile)
52echo $foo
53echo ---
54
55## STDOUT:
56begin
57---
58end
59---
60
61---
62## END
63# weird, zsh behaves differently
64## OK zsh STDOUT:
65begin
66FOO
67---
68FOO
69end
70---
71FOO
72FOO
73---
74## END
75
76
77#### < file in pipeline and subshell doesn't work
78echo FOO > file2
79
80# This only happens in command subs, which is weird
81< file2 | tr A-Z a-z
82( < file2 )
83echo end
84## STDOUT:
85end
86## END
87
88
89#### Leading redirect in a simple command
90echo hello >$TMP/hello.txt # temporary fix
91<$TMP/hello.txt cat
92## stdout: hello
93
94#### Redirect in the middle of a simple command
95f=$TMP/out
96echo -n 1 2 '3 ' > $f
97echo -n 4 5 >> $f '6 '
98echo -n 7 >> $f 8 '9 '
99echo -n >> $f 1 2 '3 '
100echo >> $f -n 4 5 '6'
101
102cat $f
103echo
104## STDOUT:
1051 2 3 4 5 6 7 8 9 1 2 3 4 5 6
106## END
107
108#### Redirect in command sub
109FOO=$(echo foo 1>&2)
110echo $FOO
111## stdout:
112## stderr: foo
113
114#### Redirect in the middle of two assignments
115FOO=foo >$TMP/out.txt BAR=bar printenv.py FOO BAR
116tac $TMP/out.txt
117## STDOUT:
118bar
119foo
120## END
121
122#### Redirect in assignment
123# dash captures stderr to a file here, which seems correct. Bash doesn't and
124# just lets it go to actual stderr.
125# For now we agree with dash/mksh, since it involves fewer special cases in the
126# code.
127
128FOO=$(echo foo 1>&2) 2>$TMP/no-command.txt
129echo FILE=
130cat $TMP/no-command.txt
131echo "FOO=$FOO"
132## STDOUT:
133FILE=
134foo
135FOO=
136## END
137## BUG bash STDOUT:
138FILE=
139FOO=
140## END
141
142
143#### Redirect in function body
144fun() { echo hi; } 1>&2
145fun
146## STDOUT:
147## END
148## STDERR:
149hi
150## END
151
152#### Redirect in function body is evaluated multiple times
153i=0
154fun() { echo "file $i"; } 1> "$TMP/file$((i++))"
155fun
156fun
157echo i=$i
158echo __
159cat $TMP/file0
160echo __
161cat $TMP/file1
162## STDOUT:
163i=2
164__
165file 1
166__
167file 2
168## END
169## N-I dash stdout-json: ""
170## N-I dash status: 2
171
172#### Redirect in function body AND function call
173fun() { echo hi; } 1>&2
174fun 2>&1
175## STDOUT:
176hi
177## END
178## STDERR:
179## END
180
181#### redirect bash extensions: [[ (( for ((
182
183case $SH in dash|mksh) exit ;; esac
184
185rm -f dbracket dparen for-expr
186
187[[ x = x ]] > dbracket
188
189(( 42 )) > dparen
190
191for ((x = 0; x < 1; ++x)); do
192 echo for-expr
193done > for-expr
194
195wc -l dbracket dparen for-expr
196
197## STDOUT:
1980 dbracket
1990 dparen
2001 for-expr
2011 total
202## END
203
204## N-I dash/mksh STDOUT:
205## END
206
207#### redirect if
208if true; then
209 echo if-body
210fi >out
211
212cat out
213
214## STDOUT:
215if-body
216## END
217
218#### redirect case
219case foo in
220 foo)
221 echo case-body
222 ;;
223esac > out
224
225cat out
226
227## STDOUT:
228case-body
229## END
230
231#### redirect while
232while true; do
233 echo while-body
234 break
235done > out
236
237cat out
238
239## STDOUT:
240while-body
241## END
242
243#### redirect for loop
244for i in $(seq 3)
245do
246 echo $i
247done > $TMP/redirect-for-loop.txt
248cat $TMP/redirect-for-loop.txt
249## STDOUT:
2501
2512
2523
253## END
254
255#### redirect subshell
256( echo foo ) 1>&2
257## stderr: foo
258## stdout-json: ""
259
260#### Prefix redirect for loop -- not allowed
261>$TMP/redirect2.txt for i in $(seq 3)
262do
263 echo $i
264done
265cat $TMP/redirect2.txt
266## status: 2
267## OK mksh status: 1
268
269#### Brace group redirect
270# Suffix works, but prefix does NOT work.
271# That comes from '| compound_command redirect_list' in the grammar!
272{ echo block-redirect; } > $TMP/br.txt
273cat $TMP/br.txt | wc -c
274## stdout: 15
275
276#### Redirect function stdout
277f() { echo one; echo two; }
278f > $TMP/redirect-func.txt
279cat $TMP/redirect-func.txt
280## STDOUT:
281one
282two
283## END
284
285#### Nested function stdout redirect
286# Shows that a stack is necessary.
287inner() {
288 echo i1
289 echo i2
290}
291outer() {
292 echo o1
293 inner > $TMP/inner.txt
294 echo o2
295}
296outer > $TMP/outer.txt
297cat $TMP/inner.txt
298echo --
299cat $TMP/outer.txt
300## STDOUT:
301i1
302i2
303--
304o1
305o2
306## END