]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/array.tests
Bash-4.3 patch 7
[thirdparty/bash.git] / tests / array.tests
1 # this is needed so that the bad assignments (b[]=bcde, for example) do not
2 # cause fatal shell errors when in posix mode
3 set +o posix
4
5 set +a
6 # The calls to egrep -v are to filter out builtin array variables that are
7 # automatically set and possibly contain values that vary.
8
9 # first make sure we handle the basics
10 x=()
11 echo ${x[@]}
12 unset x
13
14 # this should be an error
15 test=(first & second)
16 echo $?
17 unset test
18
19 # make sure declare -a converts an existing variable to an array
20 unset a
21 a=abcde
22 declare -a a
23 echo ${a[0]}
24
25 unset a
26 a=abcde
27 a[2]=bdef
28
29 unset b
30 declare -a b[256]
31
32 unset c[2]
33 unset c[*]
34
35 a[1]=
36
37 _ENV=/bin/true
38 x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
39
40 declare -r c[100]
41
42 echo ${a[0]} ${a[4]}
43 echo ${a[@]}
44
45 echo ${a[*]}
46
47 # this should print out values, too
48 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
49
50 unset a[7]
51 echo ${a[*]}
52
53 unset a[4]
54 echo ${a[*]}
55
56 echo ${a}
57 echo "${a}"
58 echo $a
59
60 unset a[0]
61 echo ${a}
62
63 echo ${a[@]}
64
65 a[5]="hello world"
66 echo ${a[5]}
67 echo ${#a[5]}
68
69 echo ${#a[@]}
70
71 a[4+5/2]="test expression"
72 declare a["7 + 8"]="test 2"
73 a[7 + 8]="test 2"
74 echo ${a[@]}
75
76 readonly a[5]
77 readonly a
78 # these two lines should output `declare' commands
79 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
80 declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
81 # this line should output `readonly' commands, even for arrays
82 set -o posix
83 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
84 set +o posix
85
86 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
87 d[9]="ninth element"
88
89 declare -a e[10]=test # this works in post-bash-2.05 versions
90 declare -a e[10]='(test)'
91
92 pass=/etc/passwd
93 declare -a f='("${d[@]}")'
94 b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
95
96 echo ${b[@]:2:3}
97
98 declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
99
100 a[3]="this is a test"
101
102 b[]=bcde
103 b[*]=aaa
104 echo ${b[ ]}
105
106 c[-2]=4
107 echo ${c[-4]}
108
109 d[7]=(abdedfegeee)
110
111 d=([]=abcde [1]="test test" [*]=last [-65]=negative )
112
113 unset d[12]
114 unset e[*]
115
116 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
117
118 ps1='hello'
119 unset ps1[2]
120 unset ${ps1[2]}
121
122 declare +a ps1
123 declare +a c
124
125 # the prompt should not print when using a here doc
126 read -p "array test: " -a rv <<!
127 this is a test of read using arrays
128 !
129
130 echo ${rv[0]} ${rv[4]}
131 echo ${rv[@]}
132
133 # the variable should be converted to an array when `read -a' is done
134 vv=1
135 read -a vv <<!
136 this is a test of arrays
137 !
138 echo ${vv[0]} ${vv[3]}
139 echo ${vv[@]}
140 unset vv
141
142 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
143
144 export rv
145 #set
146
147 x[4]=bbb
148 x=abde
149 echo $x
150 echo ${x[0]}
151 echo ${x[4]}
152 echo efgh | ( read x[1] ; echo ${x[1]} )
153 echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} )
154
155 # Make sure that arrays can be used to save the positional paramters verbatim
156 set -- a 'b c' d 'e f g' h
157
158 ARGV=( [0]=$0 "$@" )
159
160 for z in "${ARGV[@]}"
161 do
162 echo "$z"
163 done
164
165 echo "$0"
166 for z in "$@"
167 do
168 echo "$z"
169 done
170
171 # do various pattern removal and length tests
172 XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
173
174 xpath=( $( IFS=: ; echo $XPATH ) )
175
176 echo ${xpath[@]}
177 echo ${xpath[@]##*/}
178 echo ${xpath[0]##*/}
179 echo ${xpath[@]%%[!/]*}
180 echo ${xpath[0]%%[!/]*}
181 recho ${xpath##*/}
182 recho ${xpath%%[!/]*}
183 recho ${xpath[5]##*/}
184 recho ${xpath[5]%%[!/]*}
185
186 # let's try to make it a DOS-style path
187
188 zecho "${xpath[@]/\//\\}"
189 zecho "${xpath[@]//\//\\}"
190 zecho "${xpath[@]//[\/]/\\}"
191
192 # length of the first element of the array, since array without subscript
193 # is equivalent to referencing first element
194 echo ${#xpath} -- ${#xpath[0]}
195
196 # number of elements in the array
197 nelem=${#xpath[@]}
198 echo ${#xpath[@]} -- $nelem
199
200 # total length of all elements in the array, including space separators
201 xx="${xpath[*]}"
202 echo ${#xx}
203
204 # total length of all elements in the array
205 xx=$( IFS='' ; echo "${xpath[*]}" )
206 echo ${#xx}
207
208 unset xpath[nelem-1]
209
210 nelem=${#xpath[@]}
211 echo ${#xpath[@]} -- $nelem
212
213 # arrays and things that look like index assignments
214 array=(42 [1]=14 [2]=44)
215
216 array2=(grep [ 123 ] \*)
217
218 echo ${array[@]}
219 echo "${array2[@]}"
220
221 # arrays and implicit arithmetic evaluation
222 declare -i -a iarray
223
224 iarray=( 2+4 1+6 7+2 )
225 echo ${iarray[@]}
226
227 iarray[4]=4+1
228 echo ${iarray[@]}
229
230 # make sure assignment using the compound assignment syntax removes all
231 # of the old elements from the array value
232 barray=(old1 old2 old3 old4 old5)
233 barray=(new1 new2 new3)
234 echo "length = ${#barray[@]}"
235 echo "value = ${barray[*]}"
236
237 # make sure the array code behaves correctly with respect to unset variables
238 set -u
239 ( echo ${#narray[4]} )
240
241 ${THIS_SH} ./array1.sub
242 ${THIS_SH} ./array2.sub
243
244 # some old bugs and ksh93 compatibility tests
245 ${THIS_SH} ./array3.sub
246
247 # some compound assingment parsing problems that showed up in bash-3.1-release
248 ${THIS_SH} ./array4.sub
249
250 set +u
251 cd /tmp
252
253 touch 1=bar
254 foo=([10]="bar")
255 echo ${foo[0]}
256 rm 1=bar
257
258 cd $OLDPWD
259
260 foo=(a b c d e f g)
261 echo ${foo[@]}
262
263 # quoted reserved words are ok
264 foo=(\for \case \if \then \else)
265 echo ${foo[@]}
266
267 # quoted metacharacters are ok
268 foo=( [1]='<>' [2]='<' [3]='>' [4]='!' )
269 echo ${foo[@]}
270
271 # numbers are just words when not in a redirection context
272 foo=( 12 14 16 18 20 )
273 echo ${foo[@]}
274
275 foo=( 4414758999202 )
276 echo ${foo[@]}
277
278 # this was a bug in all versions of bash 2.x up to and including bash-2.04
279 declare -a ddd=(aaa
280 bbb)
281 echo ${ddd[@]}
282
283 # errors until post-bash-2.05a; now reserved words are OK
284 foo=(a b c for case if then else)
285
286 foo=(for case if then else)
287
288 # errors
289 metas=( <> < > ! )
290 metas=( [1]=<> [2]=< [3]=> [4]=! )
291
292 # various expansions that didn't really work right until post-bash-2.04
293 foo='abc'
294 echo ${foo[0]} ${#foo[0]}
295 echo ${foo[1]} ${#foo[1]}
296 echo ${foo[@]} ${#foo[@]}
297 echo ${foo[*]} ${#foo[*]}
298
299 foo=''
300 echo ${foo[0]} ${#foo[0]}
301 echo ${foo[1]} ${#foo[1]}
302 echo ${foo[@]} ${#foo[@]}
303 echo ${foo[*]} ${#foo[*]}
304
305 # new expansions added after bash-2.05b
306 x[0]=zero
307 x[1]=one
308 x[4]=four
309 x[10]=ten
310
311 recho ${!x[@]}
312 recho "${!x[@]}"
313 recho ${!x[*]}
314 recho "${!x[*]}"
315
316 # sparse array tests for code fixed in bash-3.0
317 unset av
318 av[1]='one'
319 av[2]=''
320
321 av[3]=three
322 av[5]=five
323 av[7]=seven
324
325 echo include null element -- expect one
326 echo ${av[@]:1:2} # what happens when we include a null element?
327 echo include unset element -- expect three five
328 echo ${av[@]:3:2} # what happens when we include an unset element?
329 echo start at unset element -- expect five seven
330 echo ${av[@]:4:2} # what happens when we start at an unset element?
331
332 echo too many elements -- expect three five seven
333 echo ${av[@]:3:5} # how about too many elements?
334
335 echo positive offset - expect five seven
336 echo ${av[@]:5:2}
337 echo negative offset to unset element - expect seven
338 echo ${av[@]: -2:2}
339
340 echo positive offset 2 - expect seven
341 echo ${av[@]: 6:2}
342 echo negative offset 2 - expect seven
343 echo ${av[@]: -1:2}
344
345 echo out-of-range offset
346 echo ${av[@]:12}
347
348 # parsing problems and other inconsistencies not fixed until post bash-3.0
349 unset x
350 declare -a x=(')' $$)
351 [ ${x[1]} -eq $$ ] || echo bad
352
353 unset x
354 declare -a x=(a b c d e)
355 echo ${x[4]}
356
357 z=([1]=one [4]=four [7]=seven [10]=ten)
358
359 echo ${#z[@]}
360
361 echo ${!z[@]}
362
363 unset x
364 declare -a x=(a \'b c\')
365
366 echo "${x[1]}"
367
368 unset x
369 declare -a x=(a 'b c')
370
371 echo "${x[1]}"
372
373 unset x
374 declare -a x=($0)
375 [ "${x[@]}" = $0 ] || echo double expansion of \$0
376 declare -a x=(\$0)
377 echo "${x[@]}"
378
379 # tests for bash-3.1 problems
380 ${THIS_SH} ./array5.sub
381
382 # tests for post-bash-3.2 problems, most fixed in bash-3.2 patches
383 ${THIS_SH} ./array6.sub
384 ${THIS_SH} ./array7.sub
385
386 ${THIS_SH} ./array8.sub
387
388 ${THIS_SH} ./array9.sub
389
390 ${THIS_SH} ./array10.sub
391
392 ${THIS_SH} ./array11.sub
393
394 ${THIS_SH} ./array12.sub
395
396 ${THIS_SH} ./array13.sub
397
398 ${THIS_SH} ./array14.sub
399
400 ${THIS_SH} ./array15.sub
401
402 ${THIS_SH} ./array16.sub