xref: /onnv-gate/usr/src/lib/libshell/common/tests/bracket.sh (revision 12068:08a39a083754)
14887Schin########################################################################
24887Schin#                                                                      #
34887Schin#               This software is part of the ast package               #
4*12068SRoger.Faulkner@Oracle.COM#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
54887Schin#                      and is licensed under the                       #
64887Schin#                  Common Public License, Version 1.0                  #
78462SApril.Chin@Sun.COM#                    by AT&T Intellectual Property                     #
84887Schin#                                                                      #
94887Schin#                A copy of the License is available at                 #
104887Schin#            http://www.opensource.org/licenses/cpl1.0.txt             #
114887Schin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
124887Schin#                                                                      #
134887Schin#              Information and Software Systems Research               #
144887Schin#                            AT&T Research                             #
154887Schin#                           Florham Park NJ                            #
164887Schin#                                                                      #
174887Schin#                  David Korn <dgk@research.att.com>                   #
184887Schin#                                                                      #
194887Schin########################################################################
204887Schinfunction err_exit
214887Schin{
224887Schin	print -u2 -n "\t"
234887Schin	print -u2 -r ${Command}[$1]: "${@:2}"
244887Schin	let Errors+=1
254887Schin}
264887Schinalias err_exit='err_exit $LINENO'
274887Schin
284887SchinCommand=${0##*/}
294887Schininteger Errors=0
3010898Sroland.mainz@nrubsig.org
3110898Sroland.mainz@nrubsig.orgtmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
3210898Sroland.mainz@nrubsig.orgtrap "cd /; rm -rf $tmp" EXIT
3310898Sroland.mainz@nrubsig.org
344887Schinnull=''
354887Schinif	[[ ! -z $null ]]
364887Schinthen	err_exit "-z: null string should be of zero length"
374887Schinfi
3810898Sroland.mainz@nrubsig.orgfile=$tmp/original
3910898Sroland.mainz@nrubsig.orgnewer_file=$tmp/newer
404887Schinif	[[ -z $file ]]
414887Schinthen	err_exit "-z: $file string should not be of zero length"
424887Schinfi
434887Schinif	[[ -a $file ]]
444887Schinthen	err_exit "-a: $file shouldn't exist"
454887Schinfi
464887Schin> $file
474887Schinif	[[ ! -a $file ]]
484887Schinthen	err_exit "-a: $file should exist"
494887Schinfi
504887Schinchmod 777 $file
514887Schinif	[[ ! -r $file ]]
524887Schinthen	err_exit "-r: $file should be readable"
534887Schinfi
544887Schinif	[[ ! -w $file ]]
554887Schinthen	err_exit "-w: $file should be writable"
564887Schinfi
574887Schinif	[[ ! -w $file ]]
584887Schinthen	err_exit "-x: $file should be executable"
594887Schinfi
604887Schinif	[[ ! -w $file || ! -r $file ]]
614887Schinthen	err_exit "-rw: $file should be readable/writable"
624887Schinfi
634887Schinif	[[ -s $file ]]
644887Schinthen	err_exit "-s: $file should be of zero size"
654887Schinfi
664887Schinif	[[ ! -f $file ]]
674887Schinthen	err_exit "-f: $file should be an ordinary file"
684887Schinfi
694887Schinif	[[  -d $file ]]
704887Schinthen	err_exit "-f: $file should not be a directory file"
714887Schinfi
724887Schinif	[[  ! -d . ]]
734887Schinthen	err_exit "-d: . should not be a directory file"
744887Schinfi
754887Schinif	[[  -f /dev/null ]]
764887Schinthen	err_exit "-f: /dev/null  should not be an ordinary file"
774887Schinfi
784887Schinchmod 000 $file
794887Schinif	[[ -r $file ]]
804887Schinthen	err_exit "-r: $file should not be readable"
814887Schinfi
824887Schinif	[[ ! -O $file ]]
834887Schinthen	err_exit "-r: $file should be owned by me"
844887Schinfi
854887Schinif	[[ -w $file ]]
864887Schinthen	err_exit "-w: $file should not be writable"
874887Schinfi
884887Schinif	[[ -w $file ]]
894887Schinthen	err_exit "-x: $file should not be executable"
904887Schinfi
914887Schinif	[[ -w $file || -r $file ]]
924887Schinthen	err_exit "-rw: $file should not be readable/writable"
934887Schinfi
944887Schinif	[[   -z x &&  -z x || ! -z x ]]
954887Schinthen	:
964887Schinelse	err_exit " wrong precedence"
974887Schinfi
984887Schinif	[[   -z x &&  (-z x || ! -z x) ]]
994887Schinthen	err_exit " () grouping not working"
1004887Schinfi
1014887Schinif	[[ foo < bar ]]
1024887Schinthen	err_exit "foo comes before bar"
1034887Schinfi
1044887Schin[[ . -ef $(pwd) ]] || err_exit ". is not $PWD"
1054887Schinset -o allexport
1064887Schin[[ -o allexport ]] || err_exit '-o: did not set allexport option'
1074887Schinif	[[ -n  $null ]]
1084887Schinthen	err_exit "'$null' has non-zero length"
1094887Schinfi
1104887Schinif	[[ ! -r /dev/fd/0 ]]
1114887Schinthen	err_exit "/dev/fd/0 not open for reading"
1124887Schinfi
1134887Schinif	[[ ! -w /dev/fd/2 ]]
1144887Schinthen	err_exit "/dev/fd/2 not open for writing"
1154887Schinfi
1164887Schinsleep 1
1178462SApril.Chin@Sun.COM> $newer_file
1188462SApril.Chin@Sun.COMif	[[ ! $file -ot $newer_file ]]
1198462SApril.Chin@Sun.COMthen	err_exit "$file should be older than $newer_file"
1204887Schinfi
1218462SApril.Chin@Sun.COMif	[[ $file -nt $newer_file ]]
1228462SApril.Chin@Sun.COMthen	err_exit "$newer_file should be newer than $file"
1234887Schinfi
12410898Sroland.mainz@nrubsig.orgif	[[ $file != $tmp/* ]]
12510898Sroland.mainz@nrubsig.orgthen	err_exit "$file should match $tmp/*"
1264887Schinfi
12710898Sroland.mainz@nrubsig.orgif	[[ $file = $tmp'/*' ]]
12810898Sroland.mainz@nrubsig.orgthen	err_exit "$file should not equal $tmp'/*'"
1294887Schinfi
1304887Schin[[ ! ( ! -z $null && ! -z x) ]]	|| err_exit "negation and grouping"
1314887Schin[[ -z '' || -z '' || -z '' ]]	|| err_exit "three ors not working"
1324887Schin[[ -z '' &&  -z '' && -z '' ]]	|| err_exit "three ors not working"
1334887Schin(exit 8)
1344887Schinif	[[ $? -ne 8 || $? -ne 8 ]]
1354887Schinthen	err_exit 'value $? within [[...]]'
1364887Schinfi
1374887Schinx='(x'
1384887Schinif	[[ '(x' != '('* ]]
1394887Schinthen	err_exit " '(x' does not match '('* within [[...]]"
1404887Schinfi
1414887Schinif	[[ '(x' != "("* ]]
1424887Schinthen	err_exit ' "(x" does not match "("* within [[...]]'
1434887Schinfi
1444887Schinif	[[ '(x' != \(* ]]
1454887Schinthen	err_exit ' "(x" does not match \(* within [[...]]'
1464887Schinfi
1474887Schinif	[[ 'x(' != *'(' ]]
1484887Schinthen	err_exit " 'x(' does not match '('* within [[...]]"
1494887Schinfi
1504887Schinif	[[ 'x&' != *'&' ]]
1514887Schinthen	err_exit " 'x&' does not match '&'* within [[...]]"
1524887Schinfi
1534887Schinif	[[ 'xy' = *'*' ]]
1544887Schinthen	err_exit " 'xy' matches *'*' within [[...]]"
1554887Schinfi
1564887Schinif	[[ 3 > 4 ]]
1574887Schinthen	err_exit '3 < 4'
1584887Schinfi
1594887Schinif	[[ 4 < 3 ]]
1604887Schinthen	err_exit '3 > 4'
1614887Schinfi
1624887Schinif	[[ 3x > 4x ]]
1634887Schinthen	err_exit '3x < 4x'
1644887Schinfi
1654887Schinx='bin|dev|?'
1664887Schincd /
1674887Schinif	[[ $(print $x) != "$x" ]]
1684887Schinthen	err_exit 'extended pattern matching on command arguments'
1694887Schinfi
1704887Schinif	[[ dev != $x ]]
1714887Schinthen	err_exit 'extended pattern matching not working on variables'
1724887Schinfi
1734887Schinif	[[ -u $SHELL ]]
1744887Schinthen	err_exit "setuid on $SHELL"
1754887Schinfi
1764887Schinif	[[ -g $SHELL ]]
1774887Schinthen	err_exit "setgid on $SHELL"
1784887Schinfi
1794887Schintest -d .  -a '(' ! -f . ')' || err_exit 'test not working'
1804887Schinif	[[ '!' != ! ]]
1814887Schinthen	err_exit 'quoting unary operator not working'
1824887Schinfi
1838462SApril.Chin@Sun.COMtest \( -n x \) -o \( -n y \) 2> /dev/null || err_exit 'test ( -n x ) -o ( -n y) not working'
1848462SApril.Chin@Sun.COMtest \( -n x \) -o -n y 2> /dev/null || err_exit 'test ( -n x ) -o -n y not working'
1854887Schinchmod 600 $file
1864887Schinexec 4> $file
1874887Schinprint -u4 foobar
1884887Schinif	[[ ! -s $file ]]
1894887Schinthen	err_exit "-s: $file should be non-zero"
1904887Schinfi
1914887Schinexec 4>&-
1924887Schinif	[[ 011 -ne 11 ]]
1934887Schinthen	err_exit "leading zeros in arithmetic compares not ignored"
1944887Schinfi
1954887Schin{
1964887Schin	set -x
1974887Schin	[[ foo > bar ]]
1984887Schin} 2> /dev/null || { set +x; err_exit "foo<bar with -x enabled" ;}
1994887Schinset +x
2004887Schin(
2014887Schin	eval "[[ (a) ]]"
2024887Schin) 2> /dev/null || err_exit "[[ (a) ]] not working"
2034887Schin> $file
2044887Schinchmod 4755 "$file"
2054887Schinif	test -u $file && test ! -u $file
2064887Schinthen	err_exit "test ! -u suidfile not working"
2074887Schinfi
2084887Schinfor i in '(' ')' '[' ']'
2094887Schindo	[[ $i == $i ]] || err_exit "[[ $i != $i ]]"
2104887Schindone
2114887Schin(
2124887Schin	[[ aaaa == {4}(a) ]] || err_exit 'aaaa != {4}(a)'
2134887Schin	[[ aaaa == {2,5}(a) ]] || err_exit 'aaaa != {2,4}(a)'
2144887Schin	[[ abcdcdabcd == {3,6}(ab|cd) ]] || err_exit 'abcdcdabcd == {3,4}(ab|cd)'
2154887Schin	[[ abcdcdabcde == {5}(ab|cd)e ]] || err_exit 'abcdcdabcd == {5}(ab|cd)e'
21610898Sroland.mainz@nrubsig.org) || err_exit 'errors with {..}(...) patterns'
2174887Schin[[ D290.2003.02.16.temp == D290.+(2003.02.16).temp* ]] || err_exit 'pattern match bug with +(...)'
2184887Schinrm -rf $file
2194887Schin{
22010898Sroland.mainz@nrubsig.org[[ -N $file ]] && err_exit 'test -N $tmp/*: st_mtime>st_atime after creat'
2214887Schinsleep 2
2224887Schinprint 'hello world'
22310898Sroland.mainz@nrubsig.org[[ -N $file ]] || err_exit 'test -N $tmp/*: st_mtime<=st_atime after write'
2244887Schinsleep 2
2254887Schinread
22610898Sroland.mainz@nrubsig.org[[ -N $file ]] && err_exit 'test -N $tmp/*: st_mtime>st_atime after read'
2274887Schin} > $file < $file
2284887Schinif	rm -rf "$file" && ln -s / "$file"
2294887Schinthen	[[ -L "$file" ]] || err_exit '-L not working'
2304887Schin	[[ -L "$file"/ ]] && err_exit '-L with file/ not working'
2314887Schinfi
2324887Schin$SHELL -c 't=1234567890; [[ $t == @({10}(\d)) ]]' 2> /dev/null || err_exit ' @({10}(\d)) pattern not working'
2334887Schin$SHELL -c '[[ att_ == ~(E)(att|cus)_.* ]]' 2> /dev/null || err_exit ' ~(E)(att|cus)_* pattern not working'
2344887Schin$SHELL -c '[[ att_ =~ (att|cus)_.* ]]' 2> /dev/null || err_exit ' =~ ere not working'
2354887Schin$SHELL -c '[[ abc =~ a(b)c ]]' 2> /dev/null || err_exit '[[ abc =~ a(b)c ]] fails'
2364887Schin$SHELL -xc '[[ abc =~  \babc\b ]]' 2> /dev/null || err_exit '[[ abc =~ \babc\b ]] fails'
2374887Schin[[ abc == ~(E)\babc\b ]] || err_exit '\b not preserved for ere when not in ()'
2384887Schin[[ abc == ~(iEi)\babc\b ]] || err_exit '\b not preserved for ~(iEi) when not in ()'
2398462SApril.Chin@Sun.COM
2408462SApril.Chin@Sun.COMe=$($SHELL -c '[ -z "" -a -z "" ]' 2>&1)
2418462SApril.Chin@Sun.COM[[ $e ]] && err_exit "[ ... ] compatibility check failed -- $e"
2428462SApril.Chin@Sun.COMi=hell
2438462SApril.Chin@Sun.COM[[ hell0 == $i[0] ]]  ||  err_exit 'pattern $i[0] interpreded as array ref'
2448462SApril.Chin@Sun.COMtest '(' = ')' && err_exit '"test ( = )" should not be true'
2458462SApril.Chin@Sun.COM[[ $($SHELL -c 'case  F in ~(Eilr)[a-z0-9#]) print ok;;esac' 2> /dev/null) == ok ]] || err_exit '~(Eilr) not working in case command'
2468462SApril.Chin@Sun.COM[[ $($SHELL -c "case  Q in ~(Fi)q |  \$'\E') print ok;;esac" 2> /dev/null) == ok ]] || err_exit '~(Fi)q | \E  not working in case command'
24710898Sroland.mainz@nrubsig.org
24810898Sroland.mainz@nrubsig.orgfor l in C en_US.ISO8859-15
24910898Sroland.mainz@nrubsig.orgdo	[[ $($SHELL -c "LC_COLLATE=$l" 2>&1) ]] && continue
25010898Sroland.mainz@nrubsig.org	export LC_COLLATE=$l
25110898Sroland.mainz@nrubsig.org	set -- \
25210898Sroland.mainz@nrubsig.org		'A'   0 1 1   0 1 1      1 0 0   1 0 0   \
25310898Sroland.mainz@nrubsig.org		'Z'   0 1 1   0 1 1      1 0 0   1 0 0   \
25410898Sroland.mainz@nrubsig.org		'/'   0 0 0   0 0 0      1 1 1   1 1 1   \
25510898Sroland.mainz@nrubsig.org		'.'   0 0 0   0 0 0      1 1 1   1 1 1   \
25610898Sroland.mainz@nrubsig.org		'_'   0 0 0   0 0 0      1 1 1   1 1 1   \
25710898Sroland.mainz@nrubsig.org		'-'   1 1 1   1 1 1      0 0 0   0 0 0   \
25810898Sroland.mainz@nrubsig.org		'%'   0 0 0   0 0 0      1 1 1   1 1 1   \
25910898Sroland.mainz@nrubsig.org		'@'   0 0 0   0 0 0      1 1 1   1 1 1   \
26010898Sroland.mainz@nrubsig.org		'!'   0 0 0   0 0 0      1 1 1   1 1 1   \
26110898Sroland.mainz@nrubsig.org		'^'   0 0 0   0 0 0      1 1 1   1 1 1   \
26210898Sroland.mainz@nrubsig.org		# retain this line #
26310898Sroland.mainz@nrubsig.org	while	(( $# >= 13 ))
26410898Sroland.mainz@nrubsig.org	do	c=$1
26510898Sroland.mainz@nrubsig.org		shift
26610898Sroland.mainz@nrubsig.org		for p in \
26710898Sroland.mainz@nrubsig.org			'[![.-.]]' \
26810898Sroland.mainz@nrubsig.org			'[![.-.][:upper:]]' \
26910898Sroland.mainz@nrubsig.org			'[![.-.]A-Z]' \
27010898Sroland.mainz@nrubsig.org			'[!-]' \
27110898Sroland.mainz@nrubsig.org			'[!-[:upper:]]' \
27210898Sroland.mainz@nrubsig.org			'[!-A-Z]' \
27310898Sroland.mainz@nrubsig.org			'[[.-.]]' \
27410898Sroland.mainz@nrubsig.org			'[[.-.][:upper:]]' \
27510898Sroland.mainz@nrubsig.org			'[[.-.]A-Z]' \
27610898Sroland.mainz@nrubsig.org			'[-]' \
27710898Sroland.mainz@nrubsig.org			'[-[:upper:]]' \
27810898Sroland.mainz@nrubsig.org			'[-A-Z]' \
27910898Sroland.mainz@nrubsig.org			# retain this line #
28010898Sroland.mainz@nrubsig.org		do	e=$1
28110898Sroland.mainz@nrubsig.org			shift
28210898Sroland.mainz@nrubsig.org			[[ $c == $p ]]
28310898Sroland.mainz@nrubsig.org			g=$?
28410898Sroland.mainz@nrubsig.org			[[ $g == $e ]] || err_exit "[[ '$c' == $p ]] for LC_COLLATE=$l failed -- expected $e, got $g"
28510898Sroland.mainz@nrubsig.org		done
28610898Sroland.mainz@nrubsig.org	done
28710898Sroland.mainz@nrubsig.orgdone
28810898Sroland.mainz@nrubsig.orginteger n
28910898Sroland.mainz@nrubsig.orgif	( : < /dev/tty ) 2>/dev/null && exec {n}< /dev/tty
29010898Sroland.mainz@nrubsig.orgthen	[[ -t  $n ]] || err_exit "[[ -t  n ]] fails when n > 9"
29110898Sroland.mainz@nrubsig.orgfi
29210898Sroland.mainz@nrubsig.orgfoo=([1]=a [2]=b [3]=c)
29310898Sroland.mainz@nrubsig.org[[ -v foo[1] ]] ||  err_exit 'foo[1] should be set'
29410898Sroland.mainz@nrubsig.org[[ ${foo[1]+x} ]] ||  err_exit '${foo[1]+x} should be x'
29510898Sroland.mainz@nrubsig.org[[ ${foo[@]+x} ]] ||  err_exit '${foo[@]+x} should be x'
29610898Sroland.mainz@nrubsig.orgunset foo[1]
29710898Sroland.mainz@nrubsig.org[[ -v foo[1] ]] && err_exit 'foo[1] should not be set'
29810898Sroland.mainz@nrubsig.org[[ ${foo[1]+x} ]] &&  err_exit '${foo[1]+x} should be empty'
29910898Sroland.mainz@nrubsig.orgbar=(a b c)
30010898Sroland.mainz@nrubsig.org[[ -v bar[1] ]]  || err_exit 'bar[1] should be set'
30110898Sroland.mainz@nrubsig.org[[ ${bar[1]+x} ]] ||  err_exit '${foo[1]+x} should be x'
30210898Sroland.mainz@nrubsig.orgunset bar[1]
30310898Sroland.mainz@nrubsig.org[[ ${bar[1]+x} ]] &&  err_exit '${foo[1]+x} should be empty'
30410898Sroland.mainz@nrubsig.org[[ -v bar ]] || err_exit 'bar should be set'
30510898Sroland.mainz@nrubsig.org[[ -v bar[1] ]] && err_exit 'bar[1] should not be set'
30610898Sroland.mainz@nrubsig.orginteger z=( 1 2 4)
30710898Sroland.mainz@nrubsig.org[[ -v z[1] ]] || err_exit 'z[1] should be set'
30810898Sroland.mainz@nrubsig.orgunset z[1]
30910898Sroland.mainz@nrubsig.org[[ -v z[1] ]] && err_exit 'z[1] should not be set'
31010898Sroland.mainz@nrubsig.orgtypeset -si y=( 1 2 4)
31110898Sroland.mainz@nrubsig.org[[ -v y[6] ]] && err_exit 'y[6] should not be set'
31210898Sroland.mainz@nrubsig.org[[ -v y[1] ]] ||  err_exit  'y[1] should be set'
31310898Sroland.mainz@nrubsig.orgunset y[1]
31410898Sroland.mainz@nrubsig.org[[ -v y[1] ]] && err_exit 'y[1] should not be set'
31510898Sroland.mainz@nrubsig.orgx=abc
31610898Sroland.mainz@nrubsig.org[[ -v x[0] ]] || err_exit  'x[0] should be set'
31710898Sroland.mainz@nrubsig.org[[ ${x[0]+x} ]] || err_exit print  '${x[0]+x} should be x'
31810898Sroland.mainz@nrubsig.org[[ -v x[3] ]] && err_exit 'x[3] should not be set'
31910898Sroland.mainz@nrubsig.org[[ ${x[3]+x} ]] && err_exit  '${x[0]+x} should be Empty'
32010898Sroland.mainz@nrubsig.orgunset x
32110898Sroland.mainz@nrubsig.org[[ ${x[@]+x} ]] && err_exit  '${x[@]+x} should be Empty'
32210898Sroland.mainz@nrubsig.orgunset x y z foo bar
3234887Schinexit $((Errors))
324