]>
Commit | Line | Data |
---|---|---|
3185942a JA |
1 | # TEST - basic declaration and assignment |
2 | typeset -A fluff | |
3 | declare -A | |
4 | ||
5 | fluff[foo]=one | |
6 | fluff[bar]=two | |
7 | ||
8 | declare -A | |
9 | declare -p fluff | |
10 | ||
11 | unset fluff[foo] | |
12 | declare -p fluff | |
13 | ||
14 | fluff[bar]=newval | |
0001803f CR |
15 | declare fluff[qux]=assigned |
16 | ||
3185942a JA |
17 | declare -p fluff |
18 | ||
19 | unset fluff | |
20 | ||
21 | # TEST - compount assignment and variable attributes | |
22 | declare -A wheat chaff | |
23 | wheat=( [zero]=0 [one]=a [two]=b [three]=c ) | |
24 | ||
25 | declare -i chaff | |
26 | chaff=( [zero]=1+4 [one]=3+7 four ) | |
27 | ||
28 | declare -A waste=( [pid]=42134 [version]=4.0-devel [source]=$0 [lineno]=$LINENO ) | |
29 | declare -r waste | |
30 | ||
31 | declare -A | |
32 | ||
33 | declare +i chaff | |
34 | chaff[hello world]=flip | |
35 | declare -p chaff | |
36 | ||
37 | # TEST - errors | |
a0c0a00f | 38 | waste[stuff]=other |
3185942a JA |
39 | unset waste |
40 | chaff[*]=12 | |
41 | chaff=( [one]=a [*]=12 ) | |
42 | ||
43 | # TEST - key expansion -- no word splitting | |
44 | chaff[hello world]=flip | |
45 | declare -p chaff | |
46 | echo ${chaff[hello world]} | |
47 | ||
48 | chaff[box]="multiple words" | |
49 | ||
50 | recho ${chaff[@]} | |
51 | recho "${chaff[@]}" | |
52 | ||
53 | recho ${chaff[*]} | |
54 | recho "${chaff[*]}" | |
55 | ||
56 | unset chaff | |
57 | declare -A chaff[200] | |
58 | declare +A chaff | |
59 | ||
60 | chaff[*]=12 | |
61 | chaff=( [one]=a [*]=12 ) | |
62 | ||
63 | # TEST - keys and values containing spaces | |
64 | unset wheat | |
65 | declare -A wheat | |
66 | wheat=([six]=6 [foo bar]="qux qix" ) | |
67 | ||
68 | declare -p wheat | |
69 | ||
70 | unset wheat | |
71 | declare -A wheat=([six]=6 [foo bar]="qux qix" ) | |
72 | ||
73 | recho ${wheat[foo bar]} | |
74 | recho "${wheat[foo bar]}" | |
75 | ||
76 | declare -p wheat | |
77 | ||
78 | # TEST - basic expansions: number of elements and value length | |
79 | unset wheat | |
80 | typeset -A wheat | |
81 | wheat=([six]=6 [foo bar]="qux qix" ) | |
82 | ||
83 | recho ${#wheat[@]} | |
84 | ||
85 | recho ${#wheat[foo bar]} | |
86 | ||
87 | # TEST - appending assignment operator | |
88 | unset wheat | |
89 | typeset -A wheat | |
90 | wheat=([six]=6 [foo bar]="qux qix" ) | |
91 | ||
92 | wheat[foo bar]+=' blat' | |
93 | ||
94 | recho ${wheat[foo bar]} | |
95 | recho "${wheat[foo bar]}" | |
96 | unset wheat | |
97 | ||
98 | flix=9 | |
99 | typeset -Ai wheat | |
100 | wheat=([six]=6 [foo bar]=flix ) | |
101 | ||
102 | wheat[foo bar]+=7 | |
103 | ||
104 | recho ${wheat[foo bar]} | |
105 | recho "${wheat[foo bar]}" | |
106 | unset flix wheat | |
107 | ||
108 | # TEST - index expansion: no word splitting or globbing | |
109 | typeset -A wheat | |
d233b485 | 110 | cd ${TMPDIR:=/tmp} |
3185942a JA |
111 | touch '[sfiri]' |
112 | wheat=([s*]=6 [foo bar]=flix ) | |
113 | ||
114 | recho ${wheat[@]} | |
115 | rm '[sfiri]' | |
116 | cd $OLDPWD | |
117 | ||
118 | # TEST -- associative array keys expansion | |
119 | unset wheat | |
120 | typeset -A wheat | |
121 | ||
122 | wheat=([six]=6 [foo bar]=flix ) | |
123 | ||
124 | recho ${!wheat[@]} | |
125 | recho "${!wheat[@]}" | |
126 | ||
127 | # TEST -- associative array pattern removal | |
128 | unset xpath | |
129 | typeset -A xpath | |
130 | ||
131 | xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin) | |
132 | xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. ) | |
133 | ||
134 | echo ${#xpath[@]} | |
135 | ||
136 | echo ${xpath[@]} | |
137 | echo ${xpath[@]##*/} | |
138 | echo ${xpath[0]##*/} | |
139 | echo ${xpath[@]%%[!/]*} | |
140 | echo ${xpath[0]%%[!/]*} | |
141 | recho ${xpath##*/} | |
142 | recho ${xpath%%[!/]*} | |
143 | recho ${xpath[five]##*/} | |
144 | recho ${xpath[five]%%[!/]*} | |
145 | ||
146 | echo ${#xpath[*]} | |
147 | ||
148 | echo ${xpath[*]} | |
149 | echo ${xpath[*]##*/} | |
150 | echo ${xpath[*]%%[!/]*} | |
151 | ||
152 | # TEST -- associative array pattern substitution | |
153 | unset xpath | |
154 | typeset -A xpath | |
155 | ||
156 | xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin) | |
157 | xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. ) | |
158 | ||
159 | echo ${#xpath[@]} | |
160 | # default element is "0" (as a string) | |
161 | echo ${#xpath} -- ${xpath["0"]} | |
162 | ||
163 | echo ${xpath[@]//\//^} | |
164 | echo "${xpath[@]//\//^}" | cat -v | |
165 | ||
166 | zecho "${xpath[@]/\//\\}" | |
167 | zecho "${xpath[@]//\//\\}" | |
168 | zecho "${xpath[@]//[\/]/\\}" | |
169 | ||
0001803f CR |
170 | # test assignment to key "0" |
171 | unset T | |
172 | declare -A T | |
173 | T='([a]=1)' | |
174 | echo "${T[@]}" | |
175 | unset T | |
176 | ||
d233b485 CR |
177 | # peculiar ksh93 semantics for unsubscripted assoc variable reference |
178 | declare -A T | |
179 | T[0]='zero' | |
180 | if [ "$T" != "${T[0]}" ]; then | |
181 | echo 'assoc.tests: $T and ${T[0]} mismatch' | |
182 | fi | |
183 | ||
3185942a JA |
184 | ${THIS_SH} ./assoc1.sub |
185 | ||
186 | ${THIS_SH} ./assoc2.sub | |
187 | ||
188 | ${THIS_SH} ./assoc3.sub | |
189 | ||
190 | ${THIS_SH} ./assoc4.sub | |
0001803f CR |
191 | |
192 | ${THIS_SH} ./assoc5.sub | |
193 | ||
194 | ${THIS_SH} ./assoc6.sub | |
ac50fbac CR |
195 | |
196 | ${THIS_SH} ./assoc7.sub | |
a0c0a00f CR |
197 | |
198 | # test converting between scalars and assoc arrays | |
199 | unset assoc | |
200 | assoc=assoc | |
201 | declare -A assoc | |
202 | declare -p assoc | |
203 | echo ${assoc[@]} | |
204 | ||
205 | # weird syntax required to append to multiple existing array elements using | |
206 | # compound assignment syntax | |
207 | unset assoc | |
208 | declare -A assoc | |
209 | assoc=( [one]=one [two]=two [three]=three ) | |
210 | assoc+=( [one]+=more [two]+=less ) | |
211 | declare -p assoc | |
212 | ||
213 | readonly -A assoc | |
214 | declare -p assoc | |
215 | ||
216 | ${THIS_SH} ./assoc8.sub | |
d233b485 CR |
217 | |
218 | # new shopt option to prevent multiple expansion of assoc array subscripts | |
219 | ${THIS_SH} ./assoc9.sub | |
220 | ||
221 | ${THIS_SH} ./assoc10.sub |