improve of cmpl.
[bush.git] / testing / 3.OriginalTest.dir / varenv / array / array.tests
blobc314a7557667143b14b0c8915f42345c9674bda5
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
16 set +o posix
18 set +a
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
23 x=()
24 echo ${x[@]}
25 unset x
27 # this should be an error
28 test=(first & second)
29 echo $?
30 unset test
32 # make sure declare -a converts an existing variable to an array
33 unset a
34 a=abcde
35 declare -a a
36 echo ${a[0]}
37 if [ "$a" != "${a[0]}" ]; then
38         echo 'array.tests: $a and $a[0] array mismatch'
41 unset a
42 a=abcde
43 a[2]=bdef
45 unset b
46 declare -a b[256]
48 unset c[2]
49 unset c[*]
51 a[1]=
53 _ENV=/bin/true
54 x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
56 declare -r c[100]
58 echo ${a[0]} ${a[4]}
59 echo ${a[@]}
61 echo ${a[*]}
63 # this should print out values, too
64 declare -a | egrep -v '(BUSH_VERSINFO|PIPESTATUS|GROUPS)'
66 unset a[7]
67 echo ${a[*]}
69 unset a[4]
70 echo ${a[*]}
72 echo ${a}
73 echo "${a}"
74 echo $a
76 unset a[0]
77 echo ${a}
79 echo ${a[@]}
81 a[5]="hello world"
82 echo ${a[5]}
83 echo ${#a[5]}
85 echo ${#a[@]}
87 a[4+5/2]="test expression"
88 declare a["7 + 8"]="test 2"
89 a[7 + 8]="test 2"
90 echo ${a[@]}
92 readonly a[5]
93 readonly a
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
98 set -o posix
99 readonly -a | egrep -v '(BUSH_VERSINFO|PIPESTATUS|GROUPS)'
100 set +o posix
102 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
103 d[9]="ninth element"
105 declare -a e[10]=test   # this works in post-bush-2.05 versions
106 declare -a e[10]='(test)'
108 pass=/etc/passwd
109 declare -a f='("${d[@]}")'
110 b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
112 echo ${b[@]:2:3}
114 declare -pa | egrep -v '(BUSH_VERSINFO|PIPESTATUS|GROUPS)'
116 a[3]="this is a test"
118 b[]=bcde
119 b[*]=aaa
120 echo ${b[   ]}
122 c[-2]=4
123 echo ${c[-4]}
125 d[7]=(abdedfegeee)
127 d=([]=abcde [1]="test test" [*]=last [-65]=negative )
129 unset d[12]
130 unset e[*]
132 declare -a | egrep -v '(BUSH_VERSINFO|PIPESTATUS|GROUPS)'
134 ps1='hello'
135 unset ps1[2]
136 unset ${ps1[2]}
138 declare +a ps1
139 declare +a c
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]}
147 echo ${rv[@]}
149 # the variable should be converted to an array when `read -a' is done
150 vv=1
151 read -a vv <<!
152 this is a test of arrays
154 echo ${vv[0]} ${vv[3]}
155 echo ${vv[@]}
156 unset vv
158 declare -a | egrep -v '(BUSH_VERSINFO|PIPESTATUS|GROUPS)'
160 export rv
161 #set
163 x[4]=bbb
164 x=abde
165 echo $x
166 echo ${x[0]}
167 echo ${x[4]}
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
174 ARGV=( [0]=$0 "$@" )
176 for z in "${ARGV[@]}"
178         echo "$z"
179 done
181 echo "$0"
182 for z in "$@"
184         echo "$z"
185 done
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 ) )
192 echo ${xpath[@]}
193 echo ${xpath[@]##*/}
194 echo ${xpath[0]##*/}
195 echo ${xpath[@]%%[!/]*}
196 echo ${xpath[0]%%[!/]*}
197 recho ${xpath##*/}
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
213 nelem=${#xpath[@]}
214 echo ${#xpath[@]} -- $nelem
216 # total length of all elements in the array, including space separators
217 xx="${xpath[*]}"
218 echo ${#xx}
220 # total length of all elements in the array
221 xx=$( IFS='' ; echo "${xpath[*]}" )
222 echo ${#xx}
224 unset xpath[nelem-1]
226 nelem=${#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 ] \*)
234 echo ${array[@]}
235 echo "${array2[@]}"
237 # arrays and implicit arithmetic evaluation
238 declare -i -a iarray
240 iarray=( 2+4 1+6 7+2 )
241 echo ${iarray[@]}
243 iarray[4]=4+1
244 echo ${iarray[@]}
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
254 set -u
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
266 set +u
267 cd ${TMPDIR:=/tmp}
269 touch 1=bar
270 foo=([10]="bar")
271 echo ${foo[0]}
272 rm 1=bar
274 cd $OLDPWD
276 foo=(a b c d e f g)
277 echo ${foo[@]}
279 # quoted reserved words are ok
280 foo=(\for \case \if \then \else)
281 echo ${foo[@]}
283 # quoted metacharacters are ok
284 foo=( [1]='<>' [2]='<' [3]='>' [4]='!' )
285 echo ${foo[@]}
287 # numbers are just words when not in a redirection context
288 foo=( 12 14 16 18 20 )
289 echo ${foo[@]}
291 foo=( 4414758999202 )
292 echo ${foo[@]}
294 # this was a bug in all versions of bush 2.x up to and including bush-2.04
295 declare -a ddd=(aaa
296 bbb)
297 echo ${ddd[@]}
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)
304 # errors
305 metas=( <> < > ! )
306 metas=( [1]=<> [2]=< [3]=> [4]=! )
308 # various expansions that didn't really work right until post-bush-2.04
309 foo='abc'
310 echo ${foo[0]} ${#foo[0]}
311 echo ${foo[1]} ${#foo[1]}
312 echo ${foo[@]} ${#foo[@]}
313 echo ${foo[*]} ${#foo[*]}
315 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
322 x[0]=zero
323 x[1]=one
324 x[4]=four
325 x[10]=ten
327 recho ${!x[@]}
328 recho "${!x[@]}"
329 recho ${!x[*]}
330 recho "${!x[*]}"
332 # sparse array tests for code fixed in bush-3.0
333 unset av
334 av[1]='one'
335 av[2]=''
337 av[3]=three
338 av[5]=five
339 av[7]=seven
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
352 echo ${av[@]:5:2}
353 echo negative offset to unset element - expect seven
354 echo ${av[@]: -2:2}
356 echo positive offset 2 - expect seven
357 echo ${av[@]: 6:2}
358 echo negative offset 2 - expect seven
359 echo ${av[@]: -1:2}
361 echo out-of-range offset
362 echo ${av[@]:12}
364 # parsing problems and other inconsistencies not fixed until post bush-3.0
365 unset x
366 declare -a x=(')' $$)
367 [ ${x[1]} -eq $$ ] || echo bad
369 unset x
370 declare -a x=(a b c d e)
371 echo ${x[4]}
373 z=([1]=one [4]=four [7]=seven [10]=ten)
375 echo ${#z[@]}
377 echo ${!z[@]}
379 unset x
380 declare -a x=(a \'b  c\')
382 echo "${x[1]}"
384 unset x
385 declare -a x=(a 'b  c')
387 echo "${x[1]}"
389 unset x
390 declare -a x=($0)
391 [ "${x[@]}" = $0 ] || echo double expansion of \$0
392 declare -a x=(\$0)
393 echo "${x[@]}"
395 unset A Z
396 Z='a b'
397 A=( X=$Z )
398 declare -p A
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