1 ########################################################################
3 # This software is part of the ast package #
4 # Copyright (c) 1982-2010 AT&T Intellectual Property #
5 # and is licensed under the #
6 # Common Public License, Version 1.0 #
7 # by AT&T Intellectual Property #
9 # A copy of the License is available at #
10 # http://www.opensource.org/licenses/cpl1.0.txt #
11 # (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) #
13 # Information and Software Systems Research #
17 # David Korn <dgk@research.att.com> #
19 ########################################################################
23 print
-u2 -r ${Command}[$1]: "${@:2}"
26 alias err_exit
='err_exit $LINENO'
31 tmp
=$
(mktemp
-dt) ||
{ err_exit mktemp
-dt failed
; exit 1; }
32 trap "cd /; rm -rf $tmp" EXIT
34 r
=readonly u
=Uppercase l
=Lowercase i
=22 i8
=10 L
=abc L5
=def uL5
=abcdef xi
=20
35 x
=export t
=tagged H
=hostname LZ5
=026 RZ5
=026 Z5
=123 lR5
=ABcdef R5
=def n
=l
36 for option
in u l i i8 L L5 LZ5 RZ5 Z5 r x H t R5 uL5 lR5 xi n
37 do typeset
-$option $option
39 (r
=newval
) 2> /dev
/null
&& err_exit
readonly attribute fails
42 then err_exit integer attributes fails
45 then err_exit integer base
8 fails
47 if [[ $u != UPPERCASE
]]
48 then err_exit uppercase fails
50 if [[ $l != lowercase
]]
51 then err_exit lowercase fails
53 if [[ $n != lowercase
]]
54 then err_exit reference variables fail
56 if [[ t
=tagged
!= $
(typeset
-t) ]]
57 then err_exit tagged fails
59 if [[ t
!= $
(typeset
+t
) ]]
60 then err_exit tagged fails
63 then err_exit zerofill fails
65 if [[ $RZ5 != 00026 ]]
66 then err_exit right zerofill fails
70 then err_exit leftjust fails
72 if [[ $L5 != "def " ]]
73 then err_exit leftjust fails
75 if [[ $uL5 != ABCDE
]]
76 then err_exit leftjust uppercase fails
78 if [[ $lR5 != bcdef
]]
79 then err_exit rightjust fails
81 if [[ $R5 != " def" ]]
82 then err_exit rightjust fails
84 if [[ $
($SHELL -c 'echo $x') != export ]]
85 then err_exit
export fails
87 if [[ $
($SHELL -c 'xi=xi+4;echo $xi') != 24 ]]
88 then err_exit
export attributes fails
90 x
=$
(foo
=abc
$SHELL <<!
92 $SHELL -c 'print \$foo'
96 then err_exit
'environment variables require re-export'
98 (typeset
+ ) > /dev
/null
2>&1 || err_exit
'typeset + not working'
99 (typeset
-L-5 buf
="A" 2>/dev
/null
)
101 then err_exit
'typeset allows negative field for left/right adjust'
106 then err_exit
'readonly $a=b not working'
108 if [[ $
(export |
grep '^PATH=') != PATH
=* ]]
109 then err_exit
'export not working'
113 size
=(typeset
-E x
=2.5)
115 string
="$(print $picture)"
116 if [[ "${string}" != *'size=( typeset -E'* ]]
117 then err_exit
'print of compound exponential variable not working'
119 sz
=(typeset
-E y
=2.2)
120 string
="$(print $sz)"
121 if [[ "${sz}" == *'typeset -E -F'* ]]
122 then err_exit
'print of exponential shows both -E and -F attributes'
124 print
'typeset -i m=48/4+1;print -- $m' > $tmp/script
127 if [[ $
($tmp/script) != 13 ]]
128 then err_exit
'attributes not cleared for script execution'
130 print
'print VAR=$VAR' > $tmp/script
132 $tmp/script > $tmp/script.1
133 [[ $
(< $tmp/script.1) == VAR
= ]] || err_exit
'typeset -L should not be inherited'
144 if (( ${#LAST} != 2 ))
145 then err_exit
'LAST!=2'
148 if (( ${#LAST} != 2 ))
149 then err_exit
'LAST!=2'
151 [[ $
(set |
grep LAST
) == LAST
=02 ]] || err_exit
"LAST not correct in set list"
155 if [[ $
(export |
grep ^foo
=) != 'foo=bar' ]]
156 then err_exit
'all export not working'
162 if [[ $
(export |
grep ^foo
=) != 'foo=bar' ]]
163 then err_exit
'all export not working with read'
165 if [[ $
(typeset |
grep PS2
) == PS2
]]
166 then err_exit
'typeset without arguments outputs names without attributes'
173 then b1
=aGVsbG8gd29ybGQ
=
174 b2
=aGVsbG8gd29ybGRoZWxsbyB3b3JsZA
==
175 else b1
=iIWTk5ZAppaZk4Q
=
176 b2
=iIWTk5ZAppaZk4SIhZOTlkCmlpmThA
==
180 [[ $x == "$z" ]] || print
-u2 'binary variable not expanding correctly'
181 [[ $
(printf "%B" x
) == $t1 ]] || err_exit
'typeset -b not working'
183 [[ $
(printf "%B" a
) == $w1 ]] || err_exit
'typeset -b -Z5 not working'
185 [[ $q == $b2 ]] || err_exit
'typeset -b not working with concatination'
186 [[ $
(printf "%B" q
) == $t1$t1 ]] || err_exit
'typeset -b concatination not working'
188 [[ $x == $b2 ]] || err_exit
'typeset -b not working with append'
189 [[ $
(printf "%B" x
) == $t1$t1 ]] || err_exit
'typeset -b append not working'
190 typeset
-b -Z20 z
=$b1
191 (( $
(printf "%B" z |
wc -c) == 20 )) || err_exit
'typeset -b -Z20 not storing 20 bytes'
197 hello worldhello worldhello world
199 [[ $v1 == "$b1" ]] || err_exit
"v1=$v1 should be $b1"
200 [[ $v2 == "$x" ]] || err_exit
"v1=$v2 should be $x"
201 if env
'!=1' >/dev
/null
2>&1
202 then [[ $
(env
'!=1' $SHELL -c 'echo ok' 2>/dev
/null
) == ok
]] || err_exit
'malformed environment terminates shell'
206 printf '12%Z34' |
read -r -N 5 var
207 [[ $var == MTIAMzQ
= ]] || err_exit
'binary files with zeros not working'
209 if command typeset
-usi var
=0xfffff 2> /dev
/null
210 then (( $var == 0xffff )) || err_exit
'unsigned short integers not working'
211 else err_exit
'typeset -usi cannot be used for unsigned short'
213 [[ $
($SHELL -c 'unset foo;typeset -Z2 foo; print ${foo:-3}' 2> /dev
/null
) == 3 ]] || err_exit
'${foo:-3} not 3 when typeset -Z2 field undefined'
214 [[ $
($SHELL -c 'unset foo;typeset -Z2 foo; print ${foo:=3}' 2> /dev
/null
) == 03 ]] || err_exit
'${foo:=-3} not 3 when typeset -Z2 foo undefined'
221 [[ $foo == hello
]] || err_exit
'export scoping problem in function'
224 [[ $
(export |
grep foo
) == 'foo=hello' ]] || err_exit
'export not working in functions'
225 [[ $
(export |
grep bar
) ]] && err_exit
'typeset -x not local'
226 [[ $
($SHELL -c 'typeset -r IFS=;print -r $(pwd)' 2> /dev
/null
) == "$(pwd)" ]] || err_exit
'readonly IFS causes command substitution to fail'
228 [[ $
(typeset
-pa) == *fred
* ]] || err_exit
'typeset -pa not working'
230 typeset
-LZ3 x
=abcd y z
=00abcd
232 [[ $y == "3 " ]] || err_exit
'-LZ3 not working for value 03'
233 [[ $x == "abc" ]] || err_exit
'-LZ3 not working for value abcd'
234 [[ $x == "abc" ]] || err_exit
'-LZ3 not working for value 00abcd'
237 [[ $
(typeset
-p z
) ]] && err_exit
"typeset -p for z undefined failed"
241 [[ $
(typeset
-p z
) == "$x" ]] || err_exit
"typeset -p for '$x' failed"
242 [[ $
(typeset
+p z
) == "${x%=*}" ]] || err_exit
"typeset +p for '$x' failed"
244 x
='typeset -a z=(a b c)'
246 [[ $
(typeset
-p z
) == "$x" ]] || err_exit
"typeset -p for '$x' failed"
247 [[ $
(typeset
+p z
) == "${x%=*}" ]] || err_exit
"typeset +p for '$x' failed"
257 [[ $
(typeset
-p z
) == "$x" ]] || err_exit
"typeset -p for '$x' failed"
258 [[ $
(typeset
+p z
) == "${x%%=*}" ]] || err_exit
"typeset +p for '$x' failed"
260 x
='typeset -A z=([bar]=bam [xyz]=bar)'
262 [[ $
(typeset
-p z
) == "$x" ]] || err_exit
"typeset -p for '$x' failed"
263 [[ $
(typeset
+p z
) == "${x%%=*}" ]] || err_exit
"typeset +p for '$x' failed"
268 [[ $
(typeset
-p z
) == "$x" ]] || err_exit
"typeset -p for '$x' failed"
269 [[ $
(typeset
+p z
) == "${x%%=*}" ]] || err_exit
"typeset +p for '$x' failed"
279 [[ $
(typeset
-p z
) == "Pt_t z=$x" ]] || err_exit
"typeset -p for type failed"
280 [[ $
(typeset
+p z
) == "Pt_t z" ]] || err_exit
"typeset +p for type failed"
287 [[ $
(foo
) == bar
=xxx
]] || err_exit
'typeset -p not working inside a function'
290 [[ $
(typeset
-p foo
) == 'typeset -L 5 foo' ]] || err_exit
'typeset -p not working for variables with attributes but without a value'
293 typeset -L6 foo=bbbbbb
294 [[ \$foo == bbbbbb ]]
296 } || err_exit
'typeset -L should not preserve old attributes'
299 typeset -R6 foo=bbbbbb
300 [[ \$foo == bbbbbb ]]
302 } 2> /dev
/null || err_exit
'typeset -R should not preserve old attributes'
304 expected
='YWJjZGVmZ2hpag=='
307 read foo
<<< 'abcdefghijklmnop'
308 [[ $foo == "$expected" ]] || err_exit
'read foo, where foo is "typeset -b -Z10" not working'
311 read -N10 foo
<<< 'abcdefghijklmnop'
312 [[ $foo == "$expected" ]] || err_exit
'read -N10 foo, where foo is "typeset -b -Z10" not working'
315 read -N10 foo
[4] <<< 'abcdefghijklmnop'
316 [[ ${foo[4]} == "$expected" ]] || err_exit
'read -N10 foo, where foo is "typeset -b -A" foo not working'
319 read -N10 foo
[4] <<< 'abcdefghijklmnop'
320 [[ ${foo[4]} == "$expected" ]] || err_exit
'read -N10 foo, where foo is "typeset -b -a" foo not working'
321 [[ $
(printf %B foo
[4]) == abcdefghij
]] || err_exit
'printf %B for binary associative array element not working'
322 [[ $
(printf %B foo
[4]) == abcdefghij
]] || err_exit
'printf %B for binary indexed array element not working'
325 $SHELL 2> /dev
/null
-c 'export foo=(bar=3)' && err_exit
'compound variables cannot be exported'
327 $SHELL -c 'builtin date' >/dev
/null
2>&1 &&
330 # check env var changes against a builtin that uses the env var
338 CMD
="date -f%H \\#$SEC"
343 "$EDT $PDT $EDT" "" "TZ=$PTZ" "" \
344 "$EDT $PDT $EDT" "" "TZ=$PTZ" "TZ=$ETZ" \
345 "$EDT $PDT $EDT" "TZ=$ETZ" "TZ=$PTZ" "TZ=$ETZ" \
346 "$PDT $EDT $PDT" "TZ=$PTZ" "" "TZ=$PTZ" \
347 "$PDT $EDT $PDT" "TZ=$PTZ" "TZ=$ETZ" "TZ=$PTZ" \
348 "$EDT $PDT $EDT" "foo=bar" "TZ=$PTZ" "TZ=$ETZ" \
352 got
=$
(print $
($SHELL -c "builtin date; $2 $CMD; $3 $CMD; $4 $CMD"))
353 [[ $got == $exp ]] || err_exit
"[ '$2' '$3' '$4' ] env sequence failed -- expected '$exp', got '$got'"
360 typeset
-H v
=/dev
/null
361 [[ $v == *nul
* ]] || err_exit
'typeset -H for /dev/null not working'