37759b5aa4abf81472b5435f33e41ef2f3e2a42a
[platform/upstream/bash.git] / tests / redir.tests
1 : ${TMPDIR:=/tmp}
2
3 export LC_ALL=C
4 export LANG=C
5
6 # catch-all for remaining untested redirection stuff
7 set +o posix
8
9 echo abc > /tmp/redir-test
10 cat /tmp/redir-test
11
12 set -o noclobber
13
14 #this should be an error
15 echo def > /tmp/redir-test
16 cat /tmp/redir-test
17
18 # but this should succeed
19 echo def > /tmp/redir-test-2
20 cat /tmp/redir-test-2
21
22 # and so should this
23 echo def >| /tmp/redir-test
24 cat /tmp/redir-test
25
26 set +o noclobber
27 rm /tmp/redir-test /tmp/redir-test-2
28
29 # this should be an error
30 z="a b"
31 cat < $z
32
33 echo "Point 1"
34
35 exec 3</etc/passwd
36 exec 4>$TMPDIR/bash-a
37 exec 5>$TMPDIR/bash-b
38 echo "Point 2"
39
40 echo to a 1>&4
41 echo to b 1>&5
42 cat $TMPDIR/bash-a
43 cat $TMPDIR/bash-b
44 exec 11</dev/null
45 echo "Point 3"
46
47 echo to a 1>&4
48 echo to b 1>&5
49 cat $TMPDIR/bash-a
50 cat $TMPDIR/bash-b
51
52 exec 11<&-
53 echo "Point 4"
54
55 exec 6<>$TMPDIR/bash-c
56 echo to c 1>&6
57 cat $TMPDIR/bash-c
58 echo "Point 5"
59
60 rm -f $TMPDIR/bash-a $TMPDIR/bash-b $TMPDIR/bash-c
61
62 #
63 # Test the effect of input buffering on the shell's input
64 #
65 ${THIS_SH} < redir1.sub
66
67 # more open, close, duplicate file descriptors
68 ${THIS_SH} ./redir3.sub < ./redir3.in1
69
70 # still more redirections
71 ${THIS_SH} ./redir4.sub < redir4.in1
72
73 # various forms of null redirection
74 testf()
75 {
76         if [ -f "$1" ]; then
77                 rm -f "$1"
78         else
79                 echo oops -- $1 not found
80         fi
81 }
82
83 > $TMPDIR/null-redir-a
84 testf $TMPDIR/null-redir-a
85
86 $EXIT > $TMPDIR/null-redir-b
87 testf $TMPDIR/null-redir-b
88
89 ( > $TMPDIR/null-redir-c )
90 testf $TMPDIR/null-redir-c
91
92 $EXIT > $TMPDIR/null-redir-d &
93 wait
94 testf $TMPDIR/null-redir-d
95
96 exit 3 | $EXIT > $TMPDIR/null-redir-e
97 echo $? -- ${PIPESTATUS[@]}
98 testf $TMPDIR/null-redir-e
99
100 exit 4 | > $TMPDIR/null-redir-f
101 echo $? -- ${PIPESTATUS[@]}
102 testf $TMPDIR/null-redir-f
103
104 > $TMPDIR/null-redir-g &
105 wait
106 testf $TMPDIR/null-redir-g
107
108 exec >$TMPDIR/null-redir-h &
109 wait
110 testf $TMPDIR/null-redir-h
111
112 # make sure async commands don't get /dev/null as stdin when an explicit
113 # input redirection is supplied
114 for x in 1 2 3; do
115         { read line ; echo $line ; } &
116         wait
117         { read line ; echo $line ; } &
118         wait
119 done << EOF
120 ab
121 cd
122 ef
123 gh
124 ij
125 kl
126 EOF
127
128 # make sure async commands get /dev/null as stdin in the absence of any
129 # input redirection
130 /bin/cat &
131 wait
132 echo $?
133
134 # make sure that loops work OK with here documents and are not run in
135 # subshells
136 while read line; do
137         echo $line
138         l2=$line
139 done << EOF
140 ab
141 cd
142 EOF
143 echo $l2
144
145 # These should not echo anything -- bug in versions before 2.04
146 ( ( echo hello 1>&3 ) 3>&1 ) >/dev/null 2>&1
147
148 ( ( echo hello 1>&3 ) 3>&1 ) >/dev/null 2>&1 | cat
149
150 # in posix mode, non-interactive shells are not allowed to perform
151 # filename expansion on input redirections, even if they expand to
152 # a single filename
153 set -o posix
154 cat < redir1.*
155
156 # test ksh93 dup-and-close (move fd) redirections
157 ${THIS_SH} ./redir5.sub
158
159 # test behavior after a write error with a builtin command
160 ${THIS_SH} ./redir6.sub
161
162 # problem with redirections using fds bash uses internally
163 : ${TMPDIR:=$TMPDIR}
164
165 trap 'rm -f $TMPDIR/bash-redir-$$' 0 1 2 3 6 15
166
167 echo before block
168 {
169         echo before redir
170         exec 10>&1
171         echo after redir
172 } > $TMPDIR/bash-redir-$$
173
174 echo after block
175
176 ${THIS_SH} ./redir7.sub
177
178 ${THIS_SH} ./redir8.sub
179
180 exec 9>&2
181 command exec 2>$TMPDIR/foo-$$
182 echo whatsis >&2
183 echo cat /tmp/foo
184 cat $TMPDIR/foo-$$
185 rm -f $TMPDIR/foo-$$
186 exec 2>&9
187 exec 9>&-
188
189 ${THIS_SH} ./redir9.sub