]>
Commit | Line | Data |
---|---|---|
8868edaf CR |
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. | |
5 | # | |
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. | |
10 | # | |
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/>. | |
13 | # | |
a0c0a00f CR |
14 | # a start at a test suite for what it means for an array to be set or unset and |
15 | # how to test that state | |
16 | typeset -A A | |
17 | A[a]=1 | |
18 | typeset -a a | |
19 | a[1]=1 | |
20 | ||
21 | if [ -v A ]; then echo assoc 1; else echo assoc 1 unset; fi | |
22 | if [ -v a ]; then echo array 1; else echo array 1 unset; fi | |
23 | ||
24 | if [ -v "${A[@]}" ]; then echo assoc 2; else echo assoc 2 unset; fi | |
25 | if [ -v "${a[@]}" ]; then echo array 2; else echo array 2 unset; fi | |
26 | ||
27 | echo ${A-unset1} | |
28 | echo ${a-unset2} | |
29 | ||
30 | echo ${A[@]-unset3} | |
31 | echo ${a[@]-unset4} | |
32 | ||
33 | echo ${#A[@]} | |
34 | echo ${#a[@]} | |
35 | ||
36 | typeset -A B | |
37 | typeset -a b | |
38 | ||
4491c030 CR |
39 | echo ${#B[@]} |
40 | echo ${#b[@]} | |
41 | ||
a0c0a00f CR |
42 | scalar1=foo |
43 | scalar2= | |
44 | ||
4491c030 | 45 | # this now checks for A[@] treating @ as a valid key - post-bash-5.1 |
a0c0a00f CR |
46 | if [ -v A[@] ]; then echo assoc A; else echo assoc A unset; fi |
47 | if [ -v a[@] ]; then echo array a; else echo array a unset; fi | |
48 | ||
4491c030 CR |
49 | if [ ${#A[@]} -gt 0 ]; then echo assoc A; else echo assoc A unset; fi |
50 | if [ ${#a[@]} -gt 0 ]; then echo array a; else echo array a unset; fi | |
51 | ||
a0c0a00f CR |
52 | if [ -v B[@] ]; then echo assoc B; else echo assoc B unset; fi |
53 | if [ -v b[@] ]; then echo array b; else echo array b unset; fi | |
54 | ||
4491c030 CR |
55 | if [ ${#B[@]} -gt 0 ]; then echo assoc B; else echo assoc B unset; fi |
56 | if [ ${#b[@]} -gt 0 ]; then echo array b; else echo array b unset; fi | |
57 | ||
a0c0a00f CR |
58 | if [ -v scalar1[@] ]; then echo scalar 1; else echo scalar 1 unset; fi |
59 | if [ -v scalar2[@] ]; then echo scalar 2; else echo scalar 2 unset; fi | |
60 | if [ -v scalar3[@] ]; then echo scalar 3; else echo scalar 3 unset; fi | |
61 | ||
62 | unset a A | |
63 | declare -A assoc=([one]=one [two]=two [three]=three) | |
64 | declare -a array=(one two three) | |
65 | ||
66 | scalar="one two three" | |
67 | scalar2= | |
68 | ||
69 | recho "${scalar[@]}" | |
70 | ||
71 | echo assoc: ${#assoc[@]} | |
72 | echo array: ${#array[@]} | |
73 | ||
74 | echo scalar: ${#scalar} | |
75 | echo scalar: ${#scalar[@]} | |
76 | ||
77 | echo scalar: ${#scalar2} | |
78 | echo scalar: ${#scalar2[@]} | |
79 | ||
80 | echo scalar: ${#scalar3} | |
81 | echo scalar: ${#scalar3[@]} | |
82 | ||
83 |