i-bash/tests/redir.tests

192 lines
3.2 KiB
Text
Raw Normal View History

2009-01-12 13:36:28 +00:00
: ${TMPDIR:=/tmp}
1997-06-05 14:59:13 +00:00
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
2009-01-12 13:36:28 +00:00
exec 4>$TMPDIR/bash-a
exec 5>$TMPDIR/bash-b
1997-06-05 14:59:13 +00:00
echo "Point 2"
echo to a 1>&4
echo to b 1>&5
2009-01-12 13:36:28 +00:00
cat $TMPDIR/bash-a
cat $TMPDIR/bash-b
1997-06-05 14:59:13 +00:00
exec 11</dev/null
echo "Point 3"
echo to a 1>&4
echo to b 1>&5
2009-01-12 13:36:28 +00:00
cat $TMPDIR/bash-a
cat $TMPDIR/bash-b
1997-06-05 14:59:13 +00:00
exec 11<&-
echo "Point 4"
2009-01-12 13:36:28 +00:00
exec 6<>$TMPDIR/bash-c
1997-06-05 14:59:13 +00:00
echo to c 1>&6
2009-01-12 13:36:28 +00:00
cat $TMPDIR/bash-c
1997-06-05 14:59:13 +00:00
echo "Point 5"
2009-01-12 13:36:28 +00:00
rm -f $TMPDIR/bash-a $TMPDIR/bash-b $TMPDIR/bash-c
1997-06-05 14:59:13 +00:00
#
# 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
}
2009-01-12 13:36:28 +00:00
> $TMPDIR/null-redir-a
testf $TMPDIR/null-redir-a
1997-06-05 14:59:13 +00:00
2009-01-12 13:36:28 +00:00
$EXIT > $TMPDIR/null-redir-b
testf $TMPDIR/null-redir-b
1997-06-05 14:59:13 +00:00
2009-01-12 13:36:28 +00:00
( > $TMPDIR/null-redir-c )
testf $TMPDIR/null-redir-c
1997-06-05 14:59:13 +00:00
2009-01-12 13:36:28 +00:00
$EXIT > $TMPDIR/null-redir-d &
1997-06-05 14:59:13 +00:00
wait
2009-01-12 13:36:28 +00:00
testf $TMPDIR/null-redir-d
1997-06-05 14:59:13 +00:00
2009-01-12 13:36:28 +00:00
exit 3 | $EXIT > $TMPDIR/null-redir-e
1997-06-05 14:59:13 +00:00
echo $? -- ${PIPESTATUS[@]}
2009-01-12 13:36:28 +00:00
testf $TMPDIR/null-redir-e
1997-06-05 14:59:13 +00:00
2009-01-12 13:36:28 +00:00
exit 4 | > $TMPDIR/null-redir-f
1997-06-05 14:59:13 +00:00
echo $? -- ${PIPESTATUS[@]}
2009-01-12 13:36:28 +00:00
testf $TMPDIR/null-redir-f
1997-06-05 14:59:13 +00:00
2009-01-12 13:36:28 +00:00
> $TMPDIR/null-redir-g &
1997-06-05 14:59:13 +00:00
wait
2009-01-12 13:36:28 +00:00
testf $TMPDIR/null-redir-g
1997-06-05 14:59:13 +00:00
2009-01-12 13:36:28 +00:00
exec >$TMPDIR/null-redir-h &
1997-06-05 14:59:13 +00:00
wait
2009-01-12 13:36:28 +00:00
testf $TMPDIR/null-redir-h
1997-06-05 14:59:13 +00:00
# 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
2000-03-17 21:46:59 +00:00
# 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
1997-06-05 14:59:13 +00:00
# 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.*
2002-07-17 14:10:11 +00:00
# test ksh93 dup-and-close (move fd) redirections
${THIS_SH} ./redir5.sub
2004-07-27 13:29:18 +00:00
# test behavior after a write error with a builtin command
${THIS_SH} ./redir6.sub
2005-12-07 14:08:12 +00:00
# problem with redirections using fds bash uses internally
2009-01-12 13:36:28 +00:00
: ${TMPDIR:=$TMPDIR}
2005-12-07 14:08:12 +00:00
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
2009-01-12 13:36:28 +00:00
${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
${THIS_SH} ./redir10.sub