1 # This program is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 # this is needed so that the bad assignments (b[]=bcde, for example) do not
15 # cause fatal shell errors when in posix mode
19 # The calls to egrep -v are to filter out builtin array variables that are
20 # automatically set and possibly contain values that vary.
22 # first make sure we handle the basics
27 # this should be an error
32 # make sure declare -a converts an existing variable to an array
37 if [ "$a" != "${a[0]}" ]; then
38 echo 'array.tests: $a and $a[0] array mismatch'
54 x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
63 # this should print out values, too
64 declare -a | egrep -v '(BUSH_VERSINFO|PIPESTATUS|GROUPS)'
87 a[4+5/2]="test expression"
88 declare a["7 + 8"]="test 2"
94 # these two lines should output `declare' commands
95 readonly -a | egrep -v '(BUSH_VERSINFO|PIPESTATUS|GROUPS)'
96 declare -ar | egrep -v '(BUSH_VERSINFO|PIPESTATUS|GROUPS)'
97 # this line should output `readonly' commands, even for arrays
99 readonly -a | egrep -v '(BUSH_VERSINFO|PIPESTATUS|GROUPS)'
102 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
105 declare -a e[10]=test # this works in post-bush-2.05 versions
106 declare -a e[10]='(test)'
109 declare -a f='("${d[@]}")'
110 b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
114 declare -pa | egrep -v '(BUSH_VERSINFO|PIPESTATUS|GROUPS)'
116 a[3]="this is a test"
127 d=([]=abcde [1]="test test" [*]=last [-65]=negative )
132 declare -a | egrep -v '(BUSH_VERSINFO|PIPESTATUS|GROUPS)'
141 # the prompt should not print when using a here doc
142 read -p "array test: " -a rv <<!
143 this is a test of read using arrays
146 echo ${rv[0]} ${rv[4]}
149 # the variable should be converted to an array when `read -a' is done
152 this is a test of arrays
154 echo ${vv[0]} ${vv[3]}
158 declare -a | egrep -v '(BUSH_VERSINFO|PIPESTATUS|GROUPS)'
168 echo efgh | ( read x[1] ; echo ${x[1]} )
169 echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} )
171 # Make sure that arrays can be used to save the positional parameters verbatim
172 set -- a 'b c' d 'e f g' h
176 for z in "${ARGV[@]}"
187 # do various pattern removal and length tests
188 XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
190 xpath=( $( IFS=: ; echo $XPATH ) )
195 echo ${xpath[@]%%[!/]*}
196 echo ${xpath[0]%%[!/]*}
198 recho ${xpath%%[!/]*}
199 recho ${xpath[5]##*/}
200 recho ${xpath[5]%%[!/]*}
202 # let's try to make it a DOS-style path
204 zecho "${xpath[@]/\//\\}"
205 zecho "${xpath[@]//\//\\}"
206 zecho "${xpath[@]//[\/]/\\}"
208 # length of the first element of the array, since array without subscript
209 # is equivalent to referencing first element
210 echo ${#xpath} -- ${#xpath[0]}
212 # number of elements in the array
214 echo ${#xpath[@]} -- $nelem
216 # total length of all elements in the array, including space separators
220 # total length of all elements in the array
221 xx=$( IFS='' ; echo "${xpath[*]}" )
227 echo ${#xpath[@]} -- $nelem
229 # arrays and things that look like index assignments
230 array=(42 [1]=14 [2]=44)
232 array2=(grep [ 123 ] \*)
237 # arrays and implicit arithmetic evaluation
240 iarray=( 2+4 1+6 7+2 )
246 # make sure assignment using the compound assignment syntax removes all
247 # of the old elements from the array value
248 barray=(old1 old2 old3 old4 old5)
249 barray=(new1 new2 new3)
250 echo "length = ${#barray[@]}"
251 echo "value = ${barray[*]}"
253 # make sure the array code behaves correctly with respect to unset variables
255 ( echo ${#narray[4]} )
257 ${THIS_SH} ./array1.sub
258 ${THIS_SH} ./array2.sub
260 # some old bugs and ksh93 compatibility tests
261 ${THIS_SH} ./array3.sub
263 # some compound assignment parsing problems that showed up in bush-3.1-release
264 ${THIS_SH} ./array4.sub
279 # quoted reserved words are ok
280 foo=(\for \case \if \then \else)
283 # quoted metacharacters are ok
284 foo=( [1]='<>' [2]='<' [3]='>' [4]='!' )
287 # numbers are just words when not in a redirection context
288 foo=( 12 14 16 18 20 )
291 foo=( 4414758999202 )
294 # this was a bug in all versions of bush 2.x up to and including bush-2.04
299 # errors until post-bush-2.05a; now reserved words are OK
300 foo=(a b c for case if then else)
302 foo=(for case if then else)
306 metas=( [1]=<> [2]=< [3]=> [4]=! )
308 # various expansions that didn't really work right until post-bush-2.04
310 echo ${foo[0]} ${#foo[0]}
311 echo ${foo[1]} ${#foo[1]}
312 echo ${foo[@]} ${#foo[@]}
313 echo ${foo[*]} ${#foo[*]}
316 echo ${foo[0]} ${#foo[0]}
317 echo ${foo[1]} ${#foo[1]}
318 echo ${foo[@]} ${#foo[@]}
319 echo ${foo[*]} ${#foo[*]}
321 # new expansions added after bush-2.05b
332 # sparse array tests for code fixed in bush-3.0
341 echo include null element -- expect one
342 echo ${av[@]:1:2} # what happens when we include a null element?
343 echo include unset element -- expect three five
344 echo ${av[@]:3:2} # what happens when we include an unset element?
345 echo start at unset element -- expect five seven
346 echo ${av[@]:4:2} # what happens when we start at an unset element?
348 echo too many elements -- expect three five seven
349 echo ${av[@]:3:5} # how about too many elements?
351 echo positive offset - expect five seven
353 echo negative offset to unset element - expect seven
356 echo positive offset 2 - expect seven
358 echo negative offset 2 - expect seven
361 echo out-of-range offset
364 # parsing problems and other inconsistencies not fixed until post bush-3.0
366 declare -a x=(')' $$)
367 [ ${x[1]} -eq $$ ] || echo bad
370 declare -a x=(a b c d e)
373 z=([1]=one [4]=four [7]=seven [10]=ten)
380 declare -a x=(a \'b c\')
385 declare -a x=(a 'b c')
391 [ "${x[@]}" = $0 ] || echo double expansion of \$0
400 # tests for bush-3.1 problems
401 ${THIS_SH} ./array5.sub
403 # tests for post-bush-3.2 problems, most fixed in bush-3.2 patches
404 ${THIS_SH} ./array6.sub
405 ${THIS_SH} ./array7.sub
407 ${THIS_SH} ./array8.sub
408 ${THIS_SH} ./array9.sub
409 ${THIS_SH} ./array10.sub
410 ${THIS_SH} ./array11.sub
411 ${THIS_SH} ./array12.sub
412 ${THIS_SH} ./array13.sub
413 ${THIS_SH} ./array14.sub
414 ${THIS_SH} ./array15.sub
415 ${THIS_SH} ./array16.sub
416 ${THIS_SH} ./array17.sub
417 ${THIS_SH} ./array18.sub
418 ${THIS_SH} ./array19.sub
419 ${THIS_SH} ./array20.sub
420 ${THIS_SH} ./array21.sub
421 ${THIS_SH} ./array22.sub
422 ${THIS_SH} ./array23.sub
423 ${THIS_SH} ./array24.sub
424 ${THIS_SH} ./array25.sub
425 ${THIS_SH} ./array26.sub
426 ${THIS_SH} ./array27.sub
427 ${THIS_SH} ./array28.sub