summaryrefslogtreecommitdiff
path: root/tests/redir.tests
blob: 37759b5aa4abf81472b5435f33e41ef2f3e2a42a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
: ${TMPDIR:=/tmp}

export LC_ALL=C
export LANG=C

# catch-all for remaining untested redirection stuff
set +o posix

echo abc > /tmp/redir-test
cat /tmp/redir-test

set -o noclobber

#this should be an error
echo def > /tmp/redir-test
cat /tmp/redir-test

# but this should succeed
echo def > /tmp/redir-test-2
cat /tmp/redir-test-2

# and so should this
echo def >| /tmp/redir-test
cat /tmp/redir-test

set +o noclobber
rm /tmp/redir-test /tmp/redir-test-2

# this should be an error
z="a b"
cat < $z

echo "Point 1"

exec 3</etc/passwd
exec 4>$TMPDIR/bash-a
exec 5>$TMPDIR/bash-b
echo "Point 2"

echo to a 1>&4
echo to b 1>&5
cat $TMPDIR/bash-a
cat $TMPDIR/bash-b
exec 11</dev/null
echo "Point 3"

echo to a 1>&4
echo to b 1>&5
cat $TMPDIR/bash-a
cat $TMPDIR/bash-b

exec 11<&-
echo "Point 4"

exec 6<>$TMPDIR/bash-c
echo to c 1>&6
cat $TMPDIR/bash-c
echo "Point 5"

rm -f $TMPDIR/bash-a $TMPDIR/bash-b $TMPDIR/bash-c

#
# Test the effect of input buffering on the shell's input
#
${THIS_SH} < redir1.sub

# more open, close, duplicate file descriptors
${THIS_SH} ./redir3.sub < ./redir3.in1

# still more redirections
${THIS_SH} ./redir4.sub < redir4.in1

# various forms of null redirection
testf()
{
	if [ -f "$1" ]; then
		rm -f "$1"
	else
		echo oops -- $1 not found
	fi
}

> $TMPDIR/null-redir-a
testf $TMPDIR/null-redir-a

$EXIT > $TMPDIR/null-redir-b
testf $TMPDIR/null-redir-b

( > $TMPDIR/null-redir-c )
testf $TMPDIR/null-redir-c

$EXIT > $TMPDIR/null-redir-d &
wait
testf $TMPDIR/null-redir-d

exit 3 | $EXIT > $TMPDIR/null-redir-e
echo $? -- ${PIPESTATUS[@]}
testf $TMPDIR/null-redir-e

exit 4 | > $TMPDIR/null-redir-f
echo $? -- ${PIPESTATUS[@]}
testf $TMPDIR/null-redir-f

> $TMPDIR/null-redir-g &
wait
testf $TMPDIR/null-redir-g

exec >$TMPDIR/null-redir-h &
wait
testf $TMPDIR/null-redir-h

# make sure async commands don't get /dev/null as stdin when an explicit
# input redirection is supplied
for x in 1 2 3; do
	{ read line ; echo $line ; } &
	wait
	{ read line ; echo $line ; } &
	wait
done << EOF
ab
cd
ef
gh
ij
kl
EOF

# make sure async commands get /dev/null as stdin in the absence of any
# input redirection
/bin/cat &
wait
echo $?

# make sure that loops work OK with here documents and are not run in
# subshells
while read line; do
	echo $line
	l2=$line
done << EOF
ab
cd
EOF
echo $l2

# These should not echo anything -- bug in versions before 2.04
( ( echo hello 1>&3 ) 3>&1 ) >/dev/null 2>&1

( ( echo hello 1>&3 ) 3>&1 ) >/dev/null 2>&1 | cat

# in posix mode, non-interactive shells are not allowed to perform
# filename expansion on input redirections, even if they expand to
# a single filename
set -o posix
cat < redir1.*

# test ksh93 dup-and-close (move fd) redirections
${THIS_SH} ./redir5.sub

# test behavior after a write error with a builtin command
${THIS_SH} ./redir6.sub

# problem with redirections using fds bash uses internally
: ${TMPDIR:=$TMPDIR}

trap 'rm -f $TMPDIR/bash-redir-$$' 0 1 2 3 6 15

echo before block
{
	echo before redir
	exec 10>&1
	echo after redir
} > $TMPDIR/bash-redir-$$

echo after block

${THIS_SH} ./redir7.sub

${THIS_SH} ./redir8.sub

exec 9>&2
command exec 2>$TMPDIR/foo-$$
echo whatsis >&2
echo cat /tmp/foo
cat $TMPDIR/foo-$$
rm -f $TMPDIR/foo-$$
exec 2>&9
exec 9>&-

${THIS_SH} ./redir9.sub