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 # TEST - basic declaration and assignment
28 declare fluff[qux]=assigned
34 # TEST - compound assignment and variable attributes
35 declare -A wheat chaff
36 wheat=( [zero]=0 [one]=a [two]=b [three]=c )
39 chaff=( [zero]=1+4 [one]=3+7 four )
41 declare -A waste=( [pid]=42134 [version]=4.0-devel [source]=$0 [lineno]=$LINENO )
47 chaff[hello world]=flip
54 chaff=( [one]=a [*]=12 )
56 # TEST - key expansion -- no word splitting
57 chaff[hello world]=flip
59 echo ${chaff[hello world]}
61 chaff[box]="multiple words"
74 chaff=( [one]=a [*]=12 )
76 # TEST - keys and values containing spaces
79 wheat=([six]=6 [foo bar]="qux qix" )
84 declare -A wheat=([six]=6 [foo bar]="qux qix" )
86 recho ${wheat[foo bar]}
87 recho "${wheat[foo bar]}"
91 # TEST - basic expansions: number of elements and value length
94 wheat=([six]=6 [foo bar]="qux qix" )
98 recho ${#wheat[foo bar]}
100 # TEST - appending assignment operator
103 wheat=([six]=6 [foo bar]="qux qix" )
105 wheat[foo bar]+=' blat'
107 recho ${wheat[foo bar]}
108 recho "${wheat[foo bar]}"
113 wheat=([six]=6 [foo bar]=flix )
117 recho ${wheat[foo bar]}
118 recho "${wheat[foo bar]}"
121 # TEST - index expansion: no word splitting or globbing
125 wheat=([s*]=6 [foo bar]=flix )
131 # TEST -- associative array keys expansion
135 wheat=([six]=6 [foo bar]=flix )
140 # TEST -- associative array pattern removal
144 xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
145 xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
152 echo ${xpath[@]%%[!/]*}
153 echo ${xpath[0]%%[!/]*}
155 recho ${xpath%%[!/]*}
156 recho ${xpath[five]##*/}
157 recho ${xpath[five]%%[!/]*}
163 echo ${xpath[*]%%[!/]*}
165 # TEST -- associative array pattern substitution
169 xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
170 xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
173 # default element is "0" (as a string)
174 echo ${#xpath} -- ${xpath["0"]}
176 echo ${xpath[@]//\//^}
177 echo "${xpath[@]//\//^}" | cat -v
179 zecho "${xpath[@]/\//\\}"
180 zecho "${xpath[@]//\//\\}"
181 zecho "${xpath[@]//[\/]/\\}"
183 # test assignment to key "0"
190 # peculiar ksh93 semantics for unsubscripted assoc variable reference
193 if [ "$T" != "${T[0]}" ]; then
194 echo 'assoc.tests: $T and ${T[0]} mismatch'
197 ${THIS_SH} ./assoc1.sub
199 ${THIS_SH} ./assoc2.sub
201 ${THIS_SH} ./assoc3.sub
203 ${THIS_SH} ./assoc4.sub
205 ${THIS_SH} ./assoc5.sub
207 ${THIS_SH} ./assoc6.sub
209 ${THIS_SH} ./assoc7.sub
211 # test converting between scalars and assoc arrays
218 # weird syntax required to append to multiple existing array elements using
219 # compound assignment syntax
222 assoc=( [one]=one [two]=two [three]=three )
223 assoc+=( [one]+=more [two]+=less )
231 hash=(["key"]="value1")
233 hash=(["key"]="${hash["key"]} value2")
238 ${THIS_SH} ./assoc8.sub
240 # new shopt option to prevent multiple expansion of assoc array subscripts
241 ${THIS_SH} ./assoc9.sub
243 ${THIS_SH} ./assoc10.sub
245 # test assigning associative arrays using compound key/value pair assignments
246 ${THIS_SH} ./assoc11.sub