]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/assoc.tests
Bash-4.3 patch 7
[thirdparty/bash.git] / tests / assoc.tests
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
15 declare fluff[qux]=assigned
16
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
38 unset waste
39 chaff[*]=12
40 chaff=( [one]=a [*]=12 )
41
42 # TEST - key expansion -- no word splitting
43 chaff[hello world]=flip
44 declare -p chaff
45 echo ${chaff[hello world]}
46
47 chaff[box]="multiple words"
48
49 recho ${chaff[@]}
50 recho "${chaff[@]}"
51
52 recho ${chaff[*]}
53 recho "${chaff[*]}"
54
55 unset chaff
56 declare -A chaff[200]
57 declare +A chaff
58
59 chaff[*]=12
60 chaff=( [one]=a [*]=12 )
61
62 # TEST - keys and values containing spaces
63 unset wheat
64 declare -A wheat
65 wheat=([six]=6 [foo bar]="qux qix" )
66
67 declare -p wheat
68
69 unset wheat
70 declare -A wheat=([six]=6 [foo bar]="qux qix" )
71
72 recho ${wheat[foo bar]}
73 recho "${wheat[foo bar]}"
74
75 declare -p wheat
76
77 # TEST - basic expansions: number of elements and value length
78 unset wheat
79 typeset -A wheat
80 wheat=([six]=6 [foo bar]="qux qix" )
81
82 recho ${#wheat[@]}
83
84 recho ${#wheat[foo bar]}
85
86 # TEST - appending assignment operator
87 unset wheat
88 typeset -A wheat
89 wheat=([six]=6 [foo bar]="qux qix" )
90
91 wheat[foo bar]+=' blat'
92
93 recho ${wheat[foo bar]}
94 recho "${wheat[foo bar]}"
95 unset wheat
96
97 flix=9
98 typeset -Ai wheat
99 wheat=([six]=6 [foo bar]=flix )
100
101 wheat[foo bar]+=7
102
103 recho ${wheat[foo bar]}
104 recho "${wheat[foo bar]}"
105 unset flix wheat
106
107 # TEST - index expansion: no word splitting or globbing
108 typeset -A wheat
109 cd /tmp
110 touch '[sfiri]'
111 wheat=([s*]=6 [foo bar]=flix )
112
113 recho ${wheat[@]}
114 rm '[sfiri]'
115 cd $OLDPWD
116
117 # TEST -- associative array keys expansion
118 unset wheat
119 typeset -A wheat
120
121 wheat=([six]=6 [foo bar]=flix )
122
123 recho ${!wheat[@]}
124 recho "${!wheat[@]}"
125
126 # TEST -- associative array pattern removal
127 unset xpath
128 typeset -A xpath
129
130 xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
131 xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
132
133 echo ${#xpath[@]}
134
135 echo ${xpath[@]}
136 echo ${xpath[@]##*/}
137 echo ${xpath[0]##*/}
138 echo ${xpath[@]%%[!/]*}
139 echo ${xpath[0]%%[!/]*}
140 recho ${xpath##*/}
141 recho ${xpath%%[!/]*}
142 recho ${xpath[five]##*/}
143 recho ${xpath[five]%%[!/]*}
144
145 echo ${#xpath[*]}
146
147 echo ${xpath[*]}
148 echo ${xpath[*]##*/}
149 echo ${xpath[*]%%[!/]*}
150
151 # TEST -- associative array pattern substitution
152 unset xpath
153 typeset -A xpath
154
155 xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
156 xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
157
158 echo ${#xpath[@]}
159 # default element is "0" (as a string)
160 echo ${#xpath} -- ${xpath["0"]}
161
162 echo ${xpath[@]//\//^}
163 echo "${xpath[@]//\//^}" | cat -v
164
165 zecho "${xpath[@]/\//\\}"
166 zecho "${xpath[@]//\//\\}"
167 zecho "${xpath[@]//[\/]/\\}"
168
169 # test assignment to key "0"
170 unset T
171 declare -A T
172 T='([a]=1)'
173 echo "${T[@]}"
174 unset T
175
176 ${THIS_SH} ./assoc1.sub
177
178 ${THIS_SH} ./assoc2.sub
179
180 ${THIS_SH} ./assoc3.sub
181
182 ${THIS_SH} ./assoc4.sub
183
184 ${THIS_SH} ./assoc5.sub
185
186 ${THIS_SH} ./assoc6.sub
187
188 ${THIS_SH} ./assoc7.sub