]>
Commit | Line | Data |
---|---|---|
d166f048 JA |
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 | ||
ccc6cda3 JA |
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. | |
d166f048 | 8 | |
7117c2d2 JA |
9 | # first make sure we handle the basics |
10 | x=() | |
11 | echo ${x[@]} | |
12 | unset x | |
13 | ||
bb70624e JA |
14 | # this should be an error |
15 | test=(first & second) | |
16 | echo $? | |
17 | unset test | |
18 | ||
d166f048 JA |
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 | ||
ccc6cda3 JA |
25 | unset a |
26 | a=abcde | |
27 | a[2]=bdef | |
28 | ||
f73dda09 | 29 | unset b |
ccc6cda3 JA |
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 | |
d166f048 | 48 | declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
ccc6cda3 JA |
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 | echo ${a[@]} | |
73 | ||
74 | readonly a[5] | |
75 | readonly a | |
cce855bc | 76 | # these two lines should output `declare' commands |
d166f048 JA |
77 | readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
78 | declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' | |
cce855bc JA |
79 | # this line should output `readonly' commands, even for arrays |
80 | set -o posix | |
81 | readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' | |
82 | set +o posix | |
ccc6cda3 JA |
83 | |
84 | declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")' | |
85 | d[9]="ninth element" | |
86 | ||
f73dda09 | 87 | declare -a e[10]=test # this works in post-bash-2.05 versions |
ccc6cda3 JA |
88 | declare -a e[10]='(test)' |
89 | ||
90 | pass=/etc/passwd | |
91 | declare -a f='("${d[@]}")' | |
92 | b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass) | |
93 | ||
94 | echo ${b[@]:2:3} | |
95 | ||
d166f048 | 96 | declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
ccc6cda3 JA |
97 | |
98 | a[3]="this is a test" | |
99 | ||
100 | b[]=bcde | |
101 | b[*]=aaa | |
102 | echo ${b[ ]} | |
103 | ||
104 | c[-2]=4 | |
105 | echo ${c[-4]} | |
106 | ||
107 | d[7]=(abdedfegeee) | |
108 | ||
109 | d=([]=abcde [1]="test test" [*]=last [-65]=negative ) | |
110 | ||
111 | unset d[12] | |
112 | unset e[*] | |
113 | ||
d166f048 | 114 | declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
ccc6cda3 JA |
115 | |
116 | ps1='hello' | |
117 | unset ps1[2] | |
118 | unset ${ps1[2]} | |
119 | ||
120 | declare +a ps1 | |
121 | declare +a c | |
122 | ||
123 | # the prompt should not print when using a here doc | |
124 | read -p "array test: " -a rv <<! | |
125 | this is a test of read using arrays | |
126 | ! | |
127 | ||
128 | echo ${rv[0]} ${rv[4]} | |
129 | echo ${rv[@]} | |
130 | ||
cce855bc JA |
131 | # the variable should be converted to an array when `read -a' is done |
132 | vv=1 | |
133 | read -a vv <<! | |
134 | this is a test of arrays | |
135 | ! | |
136 | echo ${vv[0]} ${vv[3]} | |
137 | echo ${vv[@]} | |
138 | unset vv | |
139 | ||
d166f048 | 140 | declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
ccc6cda3 JA |
141 | |
142 | export rv | |
143 | #set | |
144 | ||
145 | x[4]=bbb | |
146 | x=abde | |
147 | echo $x | |
148 | echo ${x[0]} | |
149 | echo ${x[4]} | |
150 | echo efgh | ( read x[1] ; echo ${x[1]} ) | |
151 | echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} ) | |
152 | ||
153 | # Make sure that arrays can be used to save the positional paramters verbatim | |
154 | set -- a 'b c' d 'e f g' h | |
155 | ||
156 | ARGV=( [0]=$0 "$@" ) | |
157 | ||
158 | for z in "${ARGV[@]}" | |
159 | do | |
160 | echo "$z" | |
161 | done | |
162 | ||
163 | echo "$0" | |
164 | for z in "$@" | |
165 | do | |
166 | echo "$z" | |
167 | done | |
d166f048 JA |
168 | |
169 | # do various pattern removal and length tests | |
170 | XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin | |
171 | ||
172 | xpath=( $( IFS=: ; echo $XPATH ) ) | |
173 | ||
174 | echo ${xpath[@]} | |
175 | echo ${xpath[@]##*/} | |
176 | echo ${xpath[0]##*/} | |
177 | echo ${xpath[@]%%[!/]*} | |
178 | echo ${xpath[0]%%[!/]*} | |
7117c2d2 JA |
179 | recho ${xpath##*/} |
180 | recho ${xpath%%[!/]*} | |
181 | recho ${xpath[5]##*/} | |
182 | recho ${xpath[5]%%[!/]*} | |
d166f048 JA |
183 | |
184 | # let's try to make it a DOS-style path | |
185 | ||
186 | zecho "${xpath[@]/\//\\}" | |
187 | zecho "${xpath[@]//\//\\}" | |
188 | zecho "${xpath[@]//[\/]/\\}" | |
189 | ||
190 | # length of the first element of the array, since array without subscript | |
191 | # is equivalent to referencing first element | |
192 | echo ${#xpath} -- ${#xpath[0]} | |
193 | ||
194 | # number of elements in the array | |
195 | nelem=${#xpath[@]} | |
196 | echo ${#xpath[@]} -- $nelem | |
197 | ||
198 | # total length of all elements in the array, including space separators | |
199 | xx="${xpath[*]}" | |
200 | echo ${#xx} | |
201 | ||
202 | # total length of all elements in the array | |
203 | xx=$( IFS='' ; echo "${xpath[*]}" ) | |
204 | echo ${#xx} | |
205 | ||
206 | unset xpath[nelem-1] | |
207 | ||
208 | nelem=${#xpath[@]} | |
209 | echo ${#xpath[@]} -- $nelem | |
210 | ||
211 | # arrays and things that look like index assignments | |
212 | array=(42 [1]=14 [2]=44) | |
213 | ||
214 | array2=(grep [ 123 ] \*) | |
215 | ||
216 | echo ${array[@]} | |
217 | echo "${array2[@]}" | |
218 | ||
219 | # arrays and implicit arithmetic evaluation | |
220 | declare -i -a iarray | |
221 | ||
222 | iarray=( 2+4 1+6 7+2 ) | |
223 | echo ${iarray[@]} | |
224 | ||
225 | iarray[4]=4+1 | |
226 | echo ${iarray[@]} | |
227 | ||
cce855bc JA |
228 | # make sure assignment using the compound assignment syntax removes all |
229 | # of the old elements from the array value | |
230 | barray=(old1 old2 old3 old4 old5) | |
231 | barray=(new1 new2 new3) | |
232 | echo "length = ${#barray[@]}" | |
233 | echo "value = ${barray[*]}" | |
234 | ||
d166f048 JA |
235 | # make sure the array code behaves correctly with respect to unset variables |
236 | set -u | |
237 | ( echo ${#narray[4]} ) | |
bb70624e JA |
238 | |
239 | # some old bugs and ksh93 compatibility tests | |
240 | set +u | |
241 | cd /tmp | |
242 | ||
243 | touch 1=bar | |
244 | foo=([10]="bar") | |
245 | echo ${foo[0]} | |
246 | rm 1=bar | |
247 | ||
248 | foo=(a b c d e f g) | |
249 | echo ${foo[@]} | |
250 | ||
251 | # quoted reserved words are ok | |
252 | foo=(\for \case \if \then \else) | |
253 | echo ${foo[@]} | |
254 | ||
255 | # quoted metacharacters are ok | |
256 | foo=( [1]='<>' [2]='<' [3]='>' [4]='!' ) | |
257 | echo ${foo[@]} | |
258 | ||
259 | # numbers are just words when not in a redirection context | |
260 | foo=( 12 14 16 18 20 ) | |
261 | echo ${foo[@]} | |
262 | ||
263 | foo=( 4414758999202 ) | |
264 | echo ${foo[@]} | |
265 | ||
28ef6c31 JA |
266 | # this was a bug in all versions of bash 2.x up to and including bash-2.04 |
267 | declare -a ddd=(aaa | |
268 | bbb) | |
269 | echo ${ddd[@]} | |
270 | ||
7117c2d2 | 271 | # errors until post-bash-2.05a; now reserved words are OK |
bb70624e JA |
272 | foo=(a b c for case if then else) |
273 | ||
274 | foo=(for case if then else) | |
275 | ||
7117c2d2 | 276 | # errors |
bb70624e JA |
277 | metas=( <> < > ! ) |
278 | metas=( [1]=<> [2]=< [3]=> [4]=! ) | |
28ef6c31 JA |
279 | |
280 | # various expansions that didn't really work right until post-bash-2.04 | |
281 | foo='abc' | |
282 | echo ${foo[0]} ${#foo[0]} | |
283 | echo ${foo[1]} ${#foo[1]} | |
284 | echo ${foo[@]} ${#foo[@]} | |
285 | echo ${foo[*]} ${#foo[*]} | |
286 | ||
287 | foo='' | |
288 | echo ${foo[0]} ${#foo[0]} | |
289 | echo ${foo[1]} ${#foo[1]} | |
290 | echo ${foo[@]} ${#foo[@]} | |
291 | echo ${foo[*]} ${#foo[*]} | |
b80f6443 JA |
292 | |
293 | # new expansions added after bash-2.05b | |
294 | x[0]=zero | |
295 | x[1]=one | |
296 | x[4]=four | |
297 | x[10]=ten | |
298 | ||
299 | recho ${!x[@]} | |
300 | recho "${!x[@]}" | |
301 | recho ${!x[*]} | |
302 | recho "${!x[*]}" | |
303 | ||
304 | # sparse array tests for code fixed in bash-3.0 | |
305 | unset av | |
306 | av[1]='one' | |
307 | av[2]='' | |
308 | ||
309 | av[3]=three | |
310 | av[5]=five | |
311 | av[7]=seven | |
312 | ||
313 | echo include null element -- expect one | |
314 | echo ${av[@]:1:2} # what happens when we include a null element? | |
315 | echo include unset element -- expect three five | |
316 | echo ${av[@]:3:2} # what happens when we include an unset element? | |
317 | echo start at unset element -- expect five seven | |
318 | echo ${av[@]:4:2} # what happens when we start at an unset element? | |
319 | ||
320 | echo too many elements -- expect three five seven | |
321 | echo ${av[@]:3:5} # how about too many elements? | |
322 | ||
323 | echo positive offset - expect five seven | |
324 | echo ${av[@]:5:2} | |
eb873671 | 325 | echo negative offset to unset element - expect seven |
b80f6443 JA |
326 | echo ${av[@]: -2:2} |
327 | ||
328 | echo positive offset 2 - expect seven | |
329 | echo ${av[@]: 6:2} | |
330 | echo negative offset 2 - expect seven | |
331 | echo ${av[@]: -1:2} | |
332 | ||
333 | echo out-of-range offset | |
334 | echo ${av[@]:12} |