blob: 8d5c89275f432bb35b22987c5c2ccd6a1f5323cd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# TEST - basic declaration and assignment
typeset -A fluff
declare -A
fluff[foo]=one
fluff[bar]=two
declare -A
declare -p fluff
unset fluff[foo]
declare -p fluff
fluff[bar]=newval
declare fluff[qux]=assigned
declare -p fluff
unset fluff
# TEST - compound assignment and variable attributes
declare -A wheat chaff
wheat=( [zero]=0 [one]=a [two]=b [three]=c )
declare -i chaff
chaff=( [zero]=1+4 [one]=3+7 four )
declare -A waste=( [pid]=42134 [version]=4.0-devel [source]=$0 [lineno]=$LINENO )
declare -r waste
declare -A
declare +i chaff
chaff[hello world]=flip
declare -p chaff
# TEST - errors
waste[stuff]=other
unset waste
chaff[*]=12
chaff=( [one]=a [*]=12 )
# TEST - key expansion -- no word splitting
chaff[hello world]=flip
declare -p chaff
echo ${chaff[hello world]}
chaff[box]="multiple words"
recho ${chaff[@]}
recho "${chaff[@]}"
recho ${chaff[*]}
recho "${chaff[*]}"
unset chaff
declare -A chaff[200]
declare +A chaff
chaff[*]=12
chaff=( [one]=a [*]=12 )
# TEST - keys and values containing spaces
unset wheat
declare -A wheat
wheat=([six]=6 [foo bar]="qux qix" )
declare -p wheat
unset wheat
declare -A wheat=([six]=6 [foo bar]="qux qix" )
recho ${wheat[foo bar]}
recho "${wheat[foo bar]}"
declare -p wheat
# TEST - basic expansions: number of elements and value length
unset wheat
typeset -A wheat
wheat=([six]=6 [foo bar]="qux qix" )
recho ${#wheat[@]}
recho ${#wheat[foo bar]}
# TEST - appending assignment operator
unset wheat
typeset -A wheat
wheat=([six]=6 [foo bar]="qux qix" )
wheat[foo bar]+=' blat'
recho ${wheat[foo bar]}
recho "${wheat[foo bar]}"
unset wheat
flix=9
typeset -Ai wheat
wheat=([six]=6 [foo bar]=flix )
wheat[foo bar]+=7
recho ${wheat[foo bar]}
recho "${wheat[foo bar]}"
unset flix wheat
# TEST - index expansion: no word splitting or globbing
typeset -A wheat
cd ${TMPDIR:=/tmp}
touch '[sfiri]'
wheat=([s*]=6 [foo bar]=flix )
recho ${wheat[@]}
rm '[sfiri]'
cd $OLDPWD
# TEST -- associative array keys expansion
unset wheat
typeset -A wheat
wheat=([six]=6 [foo bar]=flix )
recho ${!wheat[@]}
recho "${!wheat[@]}"
# TEST -- associative array pattern removal
unset xpath
typeset -A xpath
xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
echo ${#xpath[@]}
echo ${xpath[@]}
echo ${xpath[@]##*/}
echo ${xpath[0]##*/}
echo ${xpath[@]%%[!/]*}
echo ${xpath[0]%%[!/]*}
recho ${xpath##*/}
recho ${xpath%%[!/]*}
recho ${xpath[five]##*/}
recho ${xpath[five]%%[!/]*}
echo ${#xpath[*]}
echo ${xpath[*]}
echo ${xpath[*]##*/}
echo ${xpath[*]%%[!/]*}
# TEST -- associative array pattern substitution
unset xpath
typeset -A xpath
xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
echo ${#xpath[@]}
# default element is "0" (as a string)
echo ${#xpath} -- ${xpath["0"]}
echo ${xpath[@]//\//^}
echo "${xpath[@]//\//^}" | cat -v
zecho "${xpath[@]/\//\\}"
zecho "${xpath[@]//\//\\}"
zecho "${xpath[@]//[\/]/\\}"
# test assignment to key "0"
unset T
declare -A T
T='([a]=1)'
echo "${T[@]}"
unset T
# peculiar ksh93 semantics for unsubscripted assoc variable reference
declare -A T
T[0]='zero'
if [ "$T" != "${T[0]}" ]; then
echo 'assoc.tests: $T and ${T[0]} mismatch'
fi
${THIS_SH} ./assoc1.sub
${THIS_SH} ./assoc2.sub
${THIS_SH} ./assoc3.sub
${THIS_SH} ./assoc4.sub
${THIS_SH} ./assoc5.sub
${THIS_SH} ./assoc6.sub
${THIS_SH} ./assoc7.sub
# test converting between scalars and assoc arrays
unset assoc
assoc=assoc
declare -A assoc
declare -p assoc
echo ${assoc[@]}
# weird syntax required to append to multiple existing array elements using
# compound assignment syntax
unset assoc
declare -A assoc
assoc=( [one]=one [two]=two [three]=three )
assoc+=( [one]+=more [two]+=less )
declare -p assoc
readonly -A assoc
declare -p assoc
declare -A hash
hash=(["key"]="value1")
declare -p hash
hash=(["key"]="${hash["key"]} value2")
declare -p hash
unset hash
${THIS_SH} ./assoc8.sub
# new shopt option to prevent multiple expansion of assoc array subscripts
${THIS_SH} ./assoc9.sub
${THIS_SH} ./assoc10.sub
# test assigning associative arrays using compound key/value pair assignments
${THIS_SH} ./assoc11.sub
|