basic.sh revision da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968
########################################################################
# #
# This software is part of the ast package #
# Copyright (c) 1982-2007 AT&T Knowledge Ventures #
# and is licensed under the #
# Common Public License, Version 1.0 #
# by AT&T Knowledge Ventures #
# #
# A copy of the License is available at #
# (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) #
# #
# Information and Software Systems Research #
# AT&T Research #
# Florham Park NJ #
# #
# David Korn <dgk@research.att.com> #
# #
########################################################################
function err_exit
{
print -u2 -n "\t"
let Errors+=1
}
alias err_exit='err_exit $LINENO'
# test basic file operations like redirection, pipes, file expansion
integer Errors=0
then err_exit 'umask -S incorrect'
fi
pwd=$PWD
# optimizer bug test
> foobar
do print foobar*
done > out$$
then print -u2 "optimizer bug with file expansion"
fi
if [[ $(print */) != dir/ ]]
then err_exit 'file expansion with trailing / not working'
fi
if [[ $(print *) != dir ]]
then err_exit 'file expansion with single file not working'
fi
if [[ $(print *) != dir ]]
then err_exit 'file expansion leading . not working'
fi
x=dat1
$(date)
!
wait $!
set -- dat*
if (( $# != 5 ))
then err_exit "dat* matches only $# files"
fi
then set -- foo*
if [[ $1 != 'foo\abc' ]]
then err_exit 'foo* does not match foo\abc'
fi
fi
then set -- TT*
if (( $# < 2 ))
then err_exit 'TT* not expanding when file TT* exists'
fi
fi
cd ~- || err_exit "cd back failed"
#! $SHELL
print -r -- \$0
!
then err_exit '$0 not correct for #! script'
fi
then err_exit 'eval foo=\$bar not working'
fi
bar='foo=foo\ bar'
eval $bar
if [[ $foo != 'foo bar' ]]
then err_exit 'eval foo=\$bar, with bar="foo\ bar" not working'
fi
cd /tmp
then err_exit 'cd ../../tmp is not /tmp'
fi
!
wait $!
fi
{
print foo
print bam
then err_exit "Output file pointer not shared correctly."
fi
print foo
print bam
!
then err_exit "Script not working."
fi
then err_exit "Script | cat not working."
fi
then err_exit "Output file pointer not shared correctly."
fi
if [[ $x != $'foo\nbar' ]]
then err_exit " ( (print foo);(print bar ) failed"
fi
if [[ $x != $'foo\nbar' ]]
fi
if [[ $x != $'foo\nbar' ]]
fi
then builtin cat
else print no
fi
!
ok) ;;
*) err_exit "builtin replaces standard input pipe" ;;
esac
then err_exit '$0 not correct for . script'
fi
print ./b > ./a; print ./c > b; print ./d > c; print ./e > d; print "echo \"hello there\"" > e
chmod 755 a b c d e
x=$(./a)
if [[ $x != "hello there" ]]
then err_exit "nested scripts failed"
fi
if [[ $x != "hello there" ]]
then err_exit "scripts in subshells fail"
fi
cd ~- || err_exit "cd back failed"
if [[ $x != foo ]]
then err_exit "subshell in command substitution fails"
fi
exec 1>&-
x=$(print hello)
if [[ $x != hello ]]
then err_exit "command subsitution with stdout closed failed"
fi
cd $pwd
!
)
if [[ $x != $'\n'hello ]]
fi
if [[ $x != hello ]]
then err_exit "subshell in command substitution with 1 closed fails"
fi
print done
!
then err_exit "executing script with 0 closed fails"
fi
trap '' INT
trap 'print bad' INT
kill -s INT $$
print good
!
then err_exit "traps ignored by parent not ignored"
fi
trap - INT
read line
!
!
) != two ]]
then err_exit "standard input not positioned correctly"
fi
fi
fi
if [[ $(print x{a,b}y) != 'xay xby' ]]
then err_exit 'brace expansion not working'
fi
print $tgz)
done) != $'foo\nbar' ]]
then err_exit 'for loop subshell optimizer bug'
fi
unset a1
optbug()
{
integer i
for ((i=0; i < 3; i++))
do
(( ${#a1[@]} < 2 )) && return 0
set -- "${a1[@]}"
shift
set -A a1 -- "$@"
done
return 1
}
wait # not running --pipefile which would interfere with subsequent tests
: $(jobs -p) # required to clear jobs for next jobs -p (interactive side effect)
sleep 20 &
if [[ $(jobs -p) != $! ]]
fi
sleep 20 &
foo()
{
set -- $(jobs -p)
}
[[ $( (trap 'print alarm' ALRM; sleep 4) & sleep 2; kill -ALRM $!) == alarm ]] || err_exit 'ALRM signal not working'
[[ $($SHELL -c 'trap "" HUP; $SHELL -c "(sleep 2;kill -HUP $$)& sleep 4;print done"') != done ]] && err_exit 'ignored traps not being ignored'
[[ $($SHELL -c 'o=foobar; for x in foo bar; do (o=save);print $o;done' 2> /dev/null ) == $'foobar\nfoobar' ]] || err_exit 'for loop optimization subshell bug'
then [[ $($SHELL -c 'cat <(print foo)' 2> /dev/null) == foo ]] || err_exit 'process substitution not working'
[[ $($SHELL -c 'print $(cat <(print foo) )' 2> /dev/null) == foo ]] || err_exit 'process substitution in subshell not working'
line0
line1
line2
!!!
wait
for i in 1
line0
line1
line2
!!!
done
wait
cat /tmp/ksh'$$x 2>> /dev/null) == line1 ]] || err_exit '>() process substitution fails in for loop'
err_exit 'process substitution of compound commands not working'
fi
[[ $($SHELL -c "print foo | /tmp/ksh$$x ;:" 2> /dev/null ) == foo ]] || err_exit 'piping into script fails'
[[ $($SHELL -c 'X=1;print -r -- ${X:=$(expr "a(0)" : '"'a*(\([^)]\))')}'" 2> /dev/null) == 1 ]] || err_exit 'x=1;${x:=$(..."...")} failure'
[[ $($SHELL -c 'print -r -- ${X:=$(expr "a(0)" : '"'a*(\([^)]\))')}'" 2> /dev/null) == 0 ]] || err_exit '${x:=$(..."...")} failure'
then [[ $(cat <(print hello) ) == hello ]] || err_exit "process substitution not working outside for or while loop"
$SHELL -c '[[ $(for i in 1;do cat <(print hello);done ) == hello ]]' 2> /dev/null|| err_exit "process substitution not working in for or while loop"
fi
[[ $( print "(/tmp/ksh$$x bar)" | $SHELL 2>/dev/null) == 'foo bar' ]] || err_exit 'script pipe to shell fails'
exec 3<&-