]>
Commit | Line | Data |
---|---|---|
8868edaf CR |
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. | |
5 | # | |
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. | |
10 | # | |
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/>. | |
13 | # | |
d166f048 JA |
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 | |
17 | ||
ccc6cda3 JA |
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. | |
d166f048 | 21 | |
7117c2d2 JA |
22 | # first make sure we handle the basics |
23 | x=() | |
24 | echo ${x[@]} | |
25 | unset x | |
26 | ||
bb70624e JA |
27 | # this should be an error |
28 | test=(first & second) | |
29 | echo $? | |
30 | unset test | |
31 | ||
d166f048 JA |
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]} | |
d233b485 CR |
37 | if [ "$a" != "${a[0]}" ]; then |
38 | echo 'array.tests: $a and $a[0] array mismatch' | |
39 | fi | |
d166f048 | 40 | |
ccc6cda3 JA |
41 | unset a |
42 | a=abcde | |
43 | a[2]=bdef | |
44 | ||
f73dda09 | 45 | unset b |
ccc6cda3 JA |
46 | declare -a b[256] |
47 | ||
48 | unset c[2] | |
49 | unset c[*] | |
50 | ||
51 | a[1]= | |
52 | ||
53 | _ENV=/bin/true | |
54 | x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]} | |
55 | ||
56 | declare -r c[100] | |
57 | ||
58 | echo ${a[0]} ${a[4]} | |
59 | echo ${a[@]} | |
60 | ||
61 | echo ${a[*]} | |
62 | ||
63 | # this should print out values, too | |
d166f048 | 64 | declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
ccc6cda3 JA |
65 | |
66 | unset a[7] | |
67 | echo ${a[*]} | |
68 | ||
69 | unset a[4] | |
70 | echo ${a[*]} | |
71 | ||
72 | echo ${a} | |
73 | echo "${a}" | |
74 | echo $a | |
75 | ||
76 | unset a[0] | |
77 | echo ${a} | |
78 | ||
79 | echo ${a[@]} | |
80 | ||
81 | a[5]="hello world" | |
82 | echo ${a[5]} | |
83 | echo ${#a[5]} | |
84 | ||
85 | echo ${#a[@]} | |
86 | ||
87 | a[4+5/2]="test expression" | |
0001803f CR |
88 | declare a["7 + 8"]="test 2" |
89 | a[7 + 8]="test 2" | |
ccc6cda3 JA |
90 | echo ${a[@]} |
91 | ||
92 | readonly a[5] | |
93 | readonly a | |
cce855bc | 94 | # these two lines should output `declare' commands |
d166f048 JA |
95 | readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
96 | declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' | |
cce855bc JA |
97 | # this line should output `readonly' commands, even for arrays |
98 | set -o posix | |
99 | readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' | |
100 | set +o posix | |
ccc6cda3 JA |
101 | |
102 | declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")' | |
103 | d[9]="ninth element" | |
104 | ||
f73dda09 | 105 | declare -a e[10]=test # this works in post-bash-2.05 versions |
ccc6cda3 JA |
106 | declare -a e[10]='(test)' |
107 | ||
108 | pass=/etc/passwd | |
109 | declare -a f='("${d[@]}")' | |
110 | b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass) | |
111 | ||
112 | echo ${b[@]:2:3} | |
113 | ||
d166f048 | 114 | declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
ccc6cda3 JA |
115 | |
116 | a[3]="this is a test" | |
117 | ||
118 | b[]=bcde | |
119 | b[*]=aaa | |
120 | echo ${b[ ]} | |
121 | ||
122 | c[-2]=4 | |
123 | echo ${c[-4]} | |
124 | ||
125 | d[7]=(abdedfegeee) | |
126 | ||
127 | d=([]=abcde [1]="test test" [*]=last [-65]=negative ) | |
128 | ||
129 | unset d[12] | |
130 | unset e[*] | |
131 | ||
d166f048 | 132 | declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
ccc6cda3 JA |
133 | |
134 | ps1='hello' | |
135 | unset ps1[2] | |
136 | unset ${ps1[2]} | |
137 | ||
138 | declare +a ps1 | |
139 | declare +a c | |
140 | ||
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 | |
144 | ! | |
145 | ||
146 | echo ${rv[0]} ${rv[4]} | |
147 | echo ${rv[@]} | |
148 | ||
cce855bc JA |
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 | |
153 | ! | |
154 | echo ${vv[0]} ${vv[3]} | |
155 | echo ${vv[@]} | |
156 | unset vv | |
157 | ||
d166f048 | 158 | declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
ccc6cda3 JA |
159 | |
160 | export rv | |
161 | #set | |
162 | ||
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]} ) | |
170 | ||
8868edaf | 171 | # Make sure that arrays can be used to save the positional parameters verbatim |
ccc6cda3 JA |
172 | set -- a 'b c' d 'e f g' h |
173 | ||
174 | ARGV=( [0]=$0 "$@" ) | |
175 | ||
176 | for z in "${ARGV[@]}" | |
177 | do | |
178 | echo "$z" | |
179 | done | |
180 | ||
181 | echo "$0" | |
182 | for z in "$@" | |
183 | do | |
184 | echo "$z" | |
185 | done | |
d166f048 JA |
186 | |
187 | # do various pattern removal and length tests | |
188 | XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin | |
189 | ||
190 | xpath=( $( IFS=: ; echo $XPATH ) ) | |
191 | ||
192 | echo ${xpath[@]} | |
193 | echo ${xpath[@]##*/} | |
194 | echo ${xpath[0]##*/} | |
195 | echo ${xpath[@]%%[!/]*} | |
196 | echo ${xpath[0]%%[!/]*} | |
7117c2d2 JA |
197 | recho ${xpath##*/} |
198 | recho ${xpath%%[!/]*} | |
199 | recho ${xpath[5]##*/} | |
200 | recho ${xpath[5]%%[!/]*} | |
d166f048 JA |
201 | |
202 | # let's try to make it a DOS-style path | |
203 | ||
204 | zecho "${xpath[@]/\//\\}" | |
205 | zecho "${xpath[@]//\//\\}" | |
206 | zecho "${xpath[@]//[\/]/\\}" | |
207 | ||
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]} | |
211 | ||
212 | # number of elements in the array | |
213 | nelem=${#xpath[@]} | |
214 | echo ${#xpath[@]} -- $nelem | |
215 | ||
216 | # total length of all elements in the array, including space separators | |
217 | xx="${xpath[*]}" | |
218 | echo ${#xx} | |
219 | ||
220 | # total length of all elements in the array | |
221 | xx=$( IFS='' ; echo "${xpath[*]}" ) | |
222 | echo ${#xx} | |
223 | ||
224 | unset xpath[nelem-1] | |
225 | ||
226 | nelem=${#xpath[@]} | |
227 | echo ${#xpath[@]} -- $nelem | |
228 | ||
229 | # arrays and things that look like index assignments | |
230 | array=(42 [1]=14 [2]=44) | |
231 | ||
232 | array2=(grep [ 123 ] \*) | |
233 | ||
234 | echo ${array[@]} | |
235 | echo "${array2[@]}" | |
236 | ||
237 | # arrays and implicit arithmetic evaluation | |
238 | declare -i -a iarray | |
239 | ||
240 | iarray=( 2+4 1+6 7+2 ) | |
241 | echo ${iarray[@]} | |
242 | ||
243 | iarray[4]=4+1 | |
244 | echo ${iarray[@]} | |
245 | ||
cce855bc JA |
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[*]}" | |
252 | ||
d166f048 JA |
253 | # make sure the array code behaves correctly with respect to unset variables |
254 | set -u | |
255 | ( echo ${#narray[4]} ) | |
bb70624e | 256 | |
95732b49 JA |
257 | ${THIS_SH} ./array1.sub |
258 | ${THIS_SH} ./array2.sub | |
259 | ||
bb70624e | 260 | # some old bugs and ksh93 compatibility tests |
95732b49 JA |
261 | ${THIS_SH} ./array3.sub |
262 | ||
8868edaf | 263 | # some compound assignment parsing problems that showed up in bash-3.1-release |
0628567a JA |
264 | ${THIS_SH} ./array4.sub |
265 | ||
bb70624e | 266 | set +u |
d233b485 | 267 | cd ${TMPDIR:=/tmp} |
bb70624e JA |
268 | |
269 | touch 1=bar | |
270 | foo=([10]="bar") | |
271 | echo ${foo[0]} | |
272 | rm 1=bar | |
273 | ||
3185942a JA |
274 | cd $OLDPWD |
275 | ||
bb70624e JA |
276 | foo=(a b c d e f g) |
277 | echo ${foo[@]} | |
278 | ||
279 | # quoted reserved words are ok | |
280 | foo=(\for \case \if \then \else) | |
281 | echo ${foo[@]} | |
282 | ||
283 | # quoted metacharacters are ok | |
284 | foo=( [1]='<>' [2]='<' [3]='>' [4]='!' ) | |
285 | echo ${foo[@]} | |
286 | ||
287 | # numbers are just words when not in a redirection context | |
288 | foo=( 12 14 16 18 20 ) | |
289 | echo ${foo[@]} | |
290 | ||
291 | foo=( 4414758999202 ) | |
292 | echo ${foo[@]} | |
293 | ||
28ef6c31 JA |
294 | # this was a bug in all versions of bash 2.x up to and including bash-2.04 |
295 | declare -a ddd=(aaa | |
296 | bbb) | |
297 | echo ${ddd[@]} | |
298 | ||
7117c2d2 | 299 | # errors until post-bash-2.05a; now reserved words are OK |
bb70624e JA |
300 | foo=(a b c for case if then else) |
301 | ||
302 | foo=(for case if then else) | |
303 | ||
7117c2d2 | 304 | # errors |
bb70624e JA |
305 | metas=( <> < > ! ) |
306 | metas=( [1]=<> [2]=< [3]=> [4]=! ) | |
28ef6c31 JA |
307 | |
308 | # various expansions that didn't really work right until post-bash-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[*]} | |
314 | ||
315 | foo='' | |
316 | echo ${foo[0]} ${#foo[0]} | |
317 | echo ${foo[1]} ${#foo[1]} | |
318 | echo ${foo[@]} ${#foo[@]} | |
319 | echo ${foo[*]} ${#foo[*]} | |
b80f6443 JA |
320 | |
321 | # new expansions added after bash-2.05b | |
322 | x[0]=zero | |
323 | x[1]=one | |
324 | x[4]=four | |
325 | x[10]=ten | |
326 | ||
327 | recho ${!x[@]} | |
328 | recho "${!x[@]}" | |
329 | recho ${!x[*]} | |
330 | recho "${!x[*]}" | |
331 | ||
332 | # sparse array tests for code fixed in bash-3.0 | |
333 | unset av | |
334 | av[1]='one' | |
335 | av[2]='' | |
336 | ||
337 | av[3]=three | |
338 | av[5]=five | |
339 | av[7]=seven | |
340 | ||
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? | |
347 | ||
348 | echo too many elements -- expect three five seven | |
349 | echo ${av[@]:3:5} # how about too many elements? | |
350 | ||
351 | echo positive offset - expect five seven | |
352 | echo ${av[@]:5:2} | |
eb873671 | 353 | echo negative offset to unset element - expect seven |
b80f6443 JA |
354 | echo ${av[@]: -2:2} |
355 | ||
356 | echo positive offset 2 - expect seven | |
357 | echo ${av[@]: 6:2} | |
358 | echo negative offset 2 - expect seven | |
359 | echo ${av[@]: -1:2} | |
360 | ||
361 | echo out-of-range offset | |
362 | echo ${av[@]:12} | |
95732b49 JA |
363 | |
364 | # parsing problems and other inconsistencies not fixed until post bash-3.0 | |
365 | unset x | |
366 | declare -a x=(')' $$) | |
367 | [ ${x[1]} -eq $$ ] || echo bad | |
368 | ||
369 | unset x | |
370 | declare -a x=(a b c d e) | |
371 | echo ${x[4]} | |
372 | ||
373 | z=([1]=one [4]=four [7]=seven [10]=ten) | |
374 | ||
375 | echo ${#z[@]} | |
376 | ||
377 | echo ${!z[@]} | |
378 | ||
379 | unset x | |
380 | declare -a x=(a \'b c\') | |
381 | ||
382 | echo "${x[1]}" | |
383 | ||
384 | unset x | |
385 | declare -a x=(a 'b c') | |
386 | ||
387 | echo "${x[1]}" | |
388 | ||
389 | unset x | |
390 | declare -a x=($0) | |
391 | [ "${x[@]}" = $0 ] || echo double expansion of \$0 | |
392 | declare -a x=(\$0) | |
393 | echo "${x[@]}" | |
394 | ||
8868edaf CR |
395 | unset A Z |
396 | Z='a b' | |
397 | A=( X=$Z ) | |
398 | declare -p A | |
399 | ||
3185942a JA |
400 | # tests for bash-3.1 problems |
401 | ${THIS_SH} ./array5.sub | |
95732b49 | 402 | |
3185942a JA |
403 | # tests for post-bash-3.2 problems, most fixed in bash-3.2 patches |
404 | ${THIS_SH} ./array6.sub | |
405 | ${THIS_SH} ./array7.sub | |
95732b49 | 406 | |
3185942a | 407 | ${THIS_SH} ./array8.sub |
3185942a | 408 | ${THIS_SH} ./array9.sub |
495aee44 | 409 | ${THIS_SH} ./array10.sub |
ac50fbac | 410 | ${THIS_SH} ./array11.sub |
ac50fbac | 411 | ${THIS_SH} ./array12.sub |
ac50fbac | 412 | ${THIS_SH} ./array13.sub |
ac50fbac | 413 | ${THIS_SH} ./array14.sub |
ac50fbac | 414 | ${THIS_SH} ./array15.sub |
ac50fbac | 415 | ${THIS_SH} ./array16.sub |
a0c0a00f CR |
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 | |
d233b485 CR |
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 | |
8868edaf | 427 | ${THIS_SH} ./array28.sub |
4491c030 CR |
428 | ${THIS_SH} ./array29.sub |
429 | ${THIS_SH} ./array30.sub |