1 #!/usr/bin/env bash
2
3 #### >&
4 echo hi 1>&2
5 ## stderr: hi
6
7 #### <&
8 # Is there a simpler test case for this?
9 echo foo > $TMP/lessamp.txt
10 exec 6< $TMP/lessamp.txt
11 read line <&6
12 echo "[$line]"
13 ## stdout: [foo]
14
15 #### Leading redirect
16 echo hello >$TMP/hello.txt # temporary fix
17 <$TMP/hello.txt cat
18 ## stdout: hello
19
20 #### Nonexistent file
21 cat <$TMP/nonexistent.txt
22 echo status=$?
23 ## stdout: status=1
24 ## OK dash stdout: status=2
25
26 #### Redirect in command sub
27 FOO=$(echo foo 1>&2)
28 echo $FOO
29 ## stdout:
30 ## stderr: foo
31
32 #### Redirect in assignment
33 # dash captures stderr to a file here, which seems correct. Bash doesn't and
34 # just lets it go to actual stderr.
35 # For now we agree with dash/mksh, since it involves fewer special cases in the
36 # code.
37
38 FOO=$(echo foo 1>&2) 2>$TMP/no-command.txt
39 echo FILE=
40 cat $TMP/no-command.txt
41 echo "FOO=$FOO"
42 ## STDOUT:
43 FILE=
44 foo
45 FOO=
46 ## END
47 ## BUG bash STDOUT:
48 FILE=
49 FOO=
50 ## END
51
52 #### Redirect in function body.
53 func() { echo hi; } 1>&2
54 func
55 ## stdout-json: ""
56 ## stderr-json: "hi\n"
57
58 #### Bad redirects in function body
59 empty=''
60 func() { echo hi; } > $empty
61 func
62 echo status=$?
63 ## stdout: status=1
64 ## OK dash stdout: status=2
65
66 #### Redirect in function body is evaluated multiple times
67 i=0
68 func() { echo "file $i"; } 1> "$TMP/file$((i++))"
69 func
70 func
71 echo i=$i
72 echo __
73 cat $TMP/file0
74 echo __
75 cat $TMP/file1
76 ## stdout-json: "i=2\n__\nfile 1\n__\nfile 2\n"
77 ## N-I dash stdout-json: ""
78 ## N-I dash status: 2
79
80 #### Redirect in function body AND function call
81 func() { echo hi; } 1>&2
82 func 2>&1
83 ## stdout-json: "hi\n"
84 ## stderr-json: ""
85
86 #### Descriptor redirect with spaces
87 # Hm this seems like a failure of lookahead! The second thing should look to a
88 # file-like thing.
89 # I think this is a posix issue.
90 # tag: posix-issue
91 echo one 1>&2
92 echo two 1 >&2
93 echo three 1>& 2
94 ## stderr-json: "one\ntwo 1\nthree\n"
95
96 #### Filename redirect with spaces
97 # This time 1 *is* a descriptor, not a word. If you add a space between 1 and
98 # >, it doesn't work.
99 echo two 1> $TMP/file-redir1.txt
100 cat $TMP/file-redir1.txt
101 ## stdout: two
102
103 #### Quoted filename redirect with spaces
104 # POSIX makes node of this
105 echo two \1 > $TMP/file-redir2.txt
106 cat $TMP/file-redir2.txt
107 ## stdout: two 1
108
109 #### Descriptor redirect with filename
110 # bash/mksh treat this like a filename, not a descriptor.
111 # dash aborts.
112 echo one 1>&$TMP/nonexistent-filename__
113 echo "status=$?"
114 ## stdout: status=1
115 ## BUG bash stdout: status=0
116 ## OK dash stdout-json: ""
117 ## OK dash status: 2
118
119 #### redirect for loop
120 for i in $(seq 3)
121 do
122 echo $i
123 done > $TMP/redirect-for-loop.txt
124 cat $TMP/redirect-for-loop.txt
125 ## stdout-json: "1\n2\n3\n"
126
127 #### redirect subshell
128 ( echo foo ) 1>&2
129 ## stderr: foo
130 ## stdout-json: ""
131
132 #### Prefix redirect for loop -- not allowed
133 >$TMP/redirect2.txt for i in $(seq 3)
134 do
135 echo $i
136 done
137 cat $TMP/redirect2.txt
138 ## status: 2
139 ## OK mksh status: 1
140
141 #### Brace group redirect
142 # Suffix works, but prefix does NOT work.
143 # That comes from '| compound_command redirect_list' in the grammar!
144 { echo block-redirect; } > $TMP/br.txt
145 cat $TMP/br.txt | wc -c
146 ## stdout: 15
147
148 #### Redirect echo to stderr, and then redirect all of stdout somewhere.
149 { echo foo 1>&2; echo 012345789; } > $TMP/block-stdout.txt
150 cat $TMP/block-stdout.txt | wc -c
151 ## stderr: foo
152 ## stdout: 10
153
154 #### Redirect in the middle of two assignments
155 FOO=foo >$TMP/out.txt BAR=bar printenv.py FOO BAR
156 tac $TMP/out.txt
157 ## stdout-json: "bar\nfoo\n"
158
159 #### Redirect in the middle of a command
160 f=$TMP/out
161 echo -n 1 2 '3 ' > $f
162 echo -n 4 5 >> $f '6 '
163 echo -n 7 >> $f 8 '9 '
164 echo -n >> $f 1 2 '3 '
165 echo >> $f -n 4 5 '6 '
166 cat $f
167 ## stdout-json: "1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 "
168
169 #### Named file descriptor
170 exec {myfd}> $TMP/named-fd.txt
171 echo named-fd-contents >& $myfd
172 cat $TMP/named-fd.txt
173 ## stdout: named-fd-contents
174 ## status: 0
175 ## N-I dash/mksh stdout-json: ""
176 ## N-I dash/mksh status: 127
177
178 #### Redirect function stdout
179 f() { echo one; echo two; }
180 f > $TMP/redirect-func.txt
181 cat $TMP/redirect-func.txt
182 ## stdout-json: "one\ntwo\n"
183
184 #### Nested function stdout redirect
185 # Shows that a stack is necessary.
186 inner() {
187 echo i1
188 echo i2
189 }
190 outer() {
191 echo o1
192 inner > $TMP/inner.txt
193 echo o2
194 }
195 outer > $TMP/outer.txt
196 cat $TMP/inner.txt
197 echo --
198 cat $TMP/outer.txt
199 ## stdout-json: "i1\ni2\n--\no1\no2\n"
200
201 #### Redirect to empty string
202 f=''
203 echo s > "$f"
204 echo "result=$?"
205 set -o errexit
206 echo s > "$f"
207 echo DONE
208 ## stdout: result=1
209 ## status: 1
210 ## OK dash stdout: result=2
211 ## OK dash status: 2
212
213 #### Redirect to file descriptor that's not open
214 # BUGS:
215 # - dash doesn't allow file descriptors greater than 9. (This is a good thing,
216 # because the bash chapter in AOSA book mentions that juggling user vs. system
217 # file descriptors is a huge pain.)
218 # - But somehow running in parallel under spec-runner.sh changes whether descriptor
219 # 3 is open. e.g. 'echo hi 1>&3'. Possibly because of /usr/bin/time. The
220 # _tmp/spec/*.task.txt file gets corrupted!
221 # - Oh this is because I use time --output-file. That opens descriptor 3. And
222 # then time forks the shell script. The file descriptor table is inherited.
223 # - You actually have to set the file descriptor to something. What do
224 # configure and debootstrap too?
225 echo hi 1>&9
226 ## status: 1
227 ## OK dash status: 2
228
229 #### Open descriptor with exec
230 # What is the point of this? ./configure scripts and debootstrap use it.
231 exec 3>&1
232 echo hi 1>&3
233 ## stdout: hi
234 ## status: 0
235
236 #### Open multiple descriptors with exec
237 # What is the point of this? ./configure scripts and debootstrap use it.
238 exec 3>&1
239 exec 4>&1
240 echo three 1>&3
241 echo four 1>&4
242 ## stdout-json: "three\nfour\n"
243 ## status: 0
244
245 #### >| to clobber
246 echo XX >| $TMP/c.txt
247 set -o noclobber
248 echo YY > $TMP/c.txt # not globber
249 echo status=$?
250 cat $TMP/c.txt
251 echo ZZ >| $TMP/c.txt
252 cat $TMP/c.txt
253 ## stdout-json: "status=1\nXX\nZZ\n"
254 ## OK dash stdout-json: "status=2\nXX\nZZ\n"
255
256 #### &> redirects stdout and stderr
257 stdout_stderr.py &> $TMP/f.txt
258 # order is indeterminate
259 grep STDOUT $TMP/f.txt >/dev/null && echo 'ok'
260 grep STDERR $TMP/f.txt >/dev/null && echo 'ok'
261 ## STDOUT:
262 ok
263 ok
264 ## END
265 ## N-I dash stdout: STDOUT
266 ## N-I dash stderr: STDERR
267 ## N-I dash status: 1
268
269 #### 1>&2- to close file descriptor
270 # NOTE: "hi\n" goes to stderr, but it's hard to test this because other shells
271 # put errors on stderr.
272 echo hi 1>&2-
273 ## stdout-json: ""
274 ## N-I dash status: 2
275 ## N-I dash stdout-json: ""
276 ## N-I mksh status: 1
277 ## N-I mksh stdout-json: ""
278
279 #### <> for read/write
280 echo first >$TMP/rw.txt
281 exec 8<>$TMP/rw.txt
282 read line <&8
283 echo line=$line
284 echo second 1>&8
285 echo CONTENTS
286 cat $TMP/rw.txt
287 ## stdout-json: "line=first\nCONTENTS\nfirst\nsecond\n"
288
289 #### &>> appends stdout and stderr
290
291 # Fix for flaky tests: dash behaves non-deterministically under load! It
292 # doesn't implement the behavior anyway so I don't care why.
293 case $SH in
294 *dash)
295 exit 1
296 ;;
297 esac
298
299 echo "ok" > $TMP/f.txt
300 stdout_stderr.py &>> $TMP/f.txt
301 grep ok $TMP/f.txt >/dev/null && echo 'ok'
302 grep STDOUT $TMP/f.txt >/dev/null && echo 'ok'
303 grep STDERR $TMP/f.txt >/dev/null && echo 'ok'
304 ## STDOUT:
305 ok
306 ok
307 ok
308 ## END
309 ## N-I dash stdout-json: ""
310 ## N-I dash status: 1
311
312 #### exec redirect then various builtins
313 exec 5>$TMP/log.txt
314 echo hi >&5
315 set -o >&5
316 echo done
317 ## STDOUT:
318 done
319 ## END
320
321 #### >$file touches a file
322 cd $TMP
323 rm -f myfile
324 test -f myfile
325 echo status=$?
326 >myfile
327 test -f myfile
328 echo status=$?
329 ## STDOUT:
330 status=1
331 status=0
332 ## END
333 # regression for OSH
334 ## stderr-json: ""
335
336 #### $(< $file) yields the contents of the file
337 # note that it doesn't do this without a command sub!
338 cd $TMP
339 echo FOO > myfile
340 foo=$(< myfile)
341 echo $foo
342 ## STDOUT:
343 FOO
344 ## END
345 ## N-I dash stdout:
346
347 #### 2>&1 with no command
348 cd $TMP
349 ( exit 42 ) # status is reset after this
350 echo status=$?
351 2>&1
352 echo status=$?
353 ## STDOUT:
354 status=42
355 status=0
356 ## END
357 ## stderr-json: ""
358
359 #### 2&>1 (is it a redirect or is it like a&>1)
360 cd $TMP
361 2&>1
362 echo status=$?
363 ## STDOUT:
364 status=127
365 ## END
366 ## OK mksh/dash STDOUT:
367 status=0
368 ## END