########################################################################
# #
# This software is part of the ast package #
# Copyright (c) 1982-2012 AT&T Intellectual Property #
# and is licensed under the #
# Eclipse Public License, Version 1.0 #
# by AT&T Intellectual Property #
# #
# A copy of the License is available at #
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
# #
# 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'
integer Errors=0
# test basic file operations like redirection, pipes, file expansion
set -- \
go+r 0000 \
go-r 0044 \
ug=r 0330 \
go+w 0000 \
go-w 0022 \
ug=w 0550 \
go+x 0000 \
go-x 0011 \
ug=x 0660 \
o= 0007
while (( $# >= 2 ))
do umask 0
umask $1
g=$(umask)
shift 2
done
then err_exit 'umask -S incorrect'
fi
pwd=$PWD
um=$(umask -S)
( umask 0777; > foobar )
> foobar
umask "$um"
# 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 9>& 1
exec 1>&-
x=$(print hello)
if [[ $x != hello ]]
then err_exit "command subsitution with stdout closed failed"
fi
exec >& 9
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
{
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 --pipefail which would interfere with subsequent tests
: $(jobs -p) # required to clear jobs for next jobs -p (interactive side effect)
sleep 20 &
pids=$!
if [[ $(jobs -p) != $! ]]
then err_exit 'jobs -p not reporting a background job'
fi
sleep 20 &
foo()
{
set -- $(jobs -p)
}
kill $pids
[[ $( (trap 'print alarm' ALRM; sleep 4) & sleep 2; kill -ALRM $!; sleep 2; wait) == 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'
line0
line1
line2
!!!
wait
for i in 1
line0
line1
line2
!!!
done
wait
cat '$tmp/scriptx 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/scriptx ;:" 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/scriptx bar)" | $SHELL 2>/dev/null) == 'foo bar' ]] || err_exit 'script pipe to shell fails'
set -o pipefail
(( $($SHELL $tmp/scriptx) > 2.0 )) && err_exit 'command substitution causes pipefail option to hang'
exec 3<&-
$SHELL -c 'x=${ print hello;}; [[ $x == hello ]]' 2> /dev/null || err_exit '${ command;} not supported'
x=${
print hello
}
[[ $x == hello ]]
$SHELL 2> /dev/null <<- \EOF || err_exit '${...} command substitution with side effects not supported '
y=bye
x=${
y=hello
print hello
}
[[ $y == $x ]]
x=${
}
[[ $x == 'hello world' ]]
[[ $x == '}hello world' ]]
f()
{
print foo
}
unset foo
[[ $(print "[${ print foo }]") == '[foo]' ]] || err_exit '${...} not working when } is followed by ]'
[[ $(print "${ print "[${ print foo }]" }") == '[foo]' ]] || err_exit 'nested ${...} not working when } is followed by ]'
unset foo
foo=$(false) > /dev/null && err_exit 'failed command substitution with redirection not returning false'
[[ $got == $expected ]] || err_exit "command substitution background process output error -- got '$got', expected '$expected'"
binfalse=$(whence -p false)
done
then [[ $(env 'x-a=y' $SHELL -c 'env | grep x-a') == *x-a=y* ]] || err_exit 'invalid environment variables not preserved'
fi
float s=SECONDS
for i in 1 2
do print $i
s=SECONDS
set -o pipefail
for ((i=0; i < 30; i++))
do print hello
sleep .1
done | $sleep 1
[[ $({ trap 'print trap' 0; print -n | $(whence -p cat); } & wait $!) == trap ]] || err_exit 'trap on exit not getting triggered'
exp=
got=$(
function fun
{
}
)
[[ $got == $exp ]] || err_exit "pipe to function with conditional fails -- expected '$exp', got '$got'"
got=$(
)
[[ $got == $exp ]] || err_exit "pipe to { ... } with conditional fails -- expected '$exp', got '$got'"
got=$(
)
[[ $got == $exp ]] || err_exit "pipe to ( ... ) with conditional fails -- expected '$exp', got '$got'"
bintrue=$(whence -p true)
set -o pipefail
for ((i=0; i < 2; i++))
do print foo
sleep 1.5
set +o pipefail
if [[ $bintrue ]]
fi
sleep 1
builtin cat
for ((n=0; n < 1000; n++))
do
> $file
if [[ ! -s $file ]]
then err_exit 'output from pipe is lost with pipe to builtin'
break;
fi
done
$SHELL -c 'kill -0 123456789123456789123456789' 2> /dev/null && err_exit 'kill not catching process id overflows'
[[ $($SHELL -c '{ cd..; print ok;}' 2> /dev/null) == ok ]] || err_exit 'command name ending in .. causes shell to abort'
$SHELL -xc '$(LD_LIBRARY_PATH=$LD_LIBRARY_PATH exec $SHELL -c :)' > /dev/null 2>&1 || err_exit "ksh -xc '(name=value exec ksh)' fails with err=$?"