1 |
#!/usr/bin/env bash |
2 |
# |
3 |
# Test the length oeprator, which dash supports. Dash doesn't support most |
4 |
# other ops. |
5 |
|
6 |
#### String length |
7 |
v=foo |
8 |
echo ${#v} |
9 |
## stdout: 3 |
10 |
|
11 |
#### Unicode string length (UTF-8) |
12 |
v=$'_\u03bc_' |
13 |
echo ${#v} |
14 |
## stdout: 3 |
15 |
## N-I dash stdout: 9 |
16 |
## N-I mksh stdout: 4 |
17 |
|
18 |
#### Unicode string length (spec/testdata/utf8-chars.txt) |
19 |
v=$(cat spec/testdata/utf8-chars.txt) |
20 |
echo ${#v} |
21 |
## stdout: 7 |
22 |
## N-I dash stdout: 13 |
23 |
## N-I mksh stdout: 13 |
24 |
|
25 |
#### String length with incomplete utf-8 |
26 |
for num_bytes in 0 1 2 3 4 5 6 7 8 9 10 11 12 13; do |
27 |
s=$(head -c $num_bytes spec/testdata/utf8-chars.txt) |
28 |
echo ${#s} |
29 |
done 2> $TMP/err.txt |
30 |
|
31 |
grep 'warning:' $TMP/err.txt |
32 |
true # exit 0 |
33 |
|
34 |
## STDOUT: |
35 |
0 |
36 |
1 |
37 |
2 |
38 |
-1 |
39 |
3 |
40 |
4 |
41 |
-1 |
42 |
-1 |
43 |
5 |
44 |
6 |
45 |
-1 |
46 |
-1 |
47 |
-1 |
48 |
7 |
49 |
[ stdin ]:3: warning: Incomplete UTF-8 character |
50 |
[ stdin ]:3: warning: Incomplete UTF-8 character |
51 |
[ stdin ]:3: warning: Incomplete UTF-8 character |
52 |
[ stdin ]:3: warning: Incomplete UTF-8 character |
53 |
[ stdin ]:3: warning: Incomplete UTF-8 character |
54 |
[ stdin ]:3: warning: Incomplete UTF-8 character |
55 |
## END |
56 |
# zsh behavior actually matches bash! |
57 |
## BUG bash/zsh stderr-json: "" |
58 |
## BUG bash/zsh STDOUT: |
59 |
0 |
60 |
1 |
61 |
2 |
62 |
3 |
63 |
3 |
64 |
4 |
65 |
5 |
66 |
6 |
67 |
5 |
68 |
6 |
69 |
7 |
70 |
8 |
71 |
9 |
72 |
7 |
73 |
## END |
74 |
## BUG dash/mksh stderr-json: "" |
75 |
## N-I dash/mksh STDOUT: |
76 |
0 |
77 |
1 |
78 |
2 |
79 |
3 |
80 |
4 |
81 |
5 |
82 |
6 |
83 |
7 |
84 |
8 |
85 |
9 |
86 |
10 |
87 |
11 |
88 |
12 |
89 |
13 |
90 |
## END |
91 |
|
92 |
#### String length with invalid utf-8 continuation bytes |
93 |
for num_bytes in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14; do |
94 |
s=$(head -c $num_bytes spec/testdata/utf8-chars.txt)$(echo -e "\xFF") |
95 |
echo ${#s} |
96 |
done 2> $TMP/err.txt |
97 |
|
98 |
grep 'warning:' $TMP/err.txt |
99 |
true |
100 |
|
101 |
## STDOUT: |
102 |
-1 |
103 |
-1 |
104 |
-1 |
105 |
-1 |
106 |
-1 |
107 |
-1 |
108 |
-1 |
109 |
-1 |
110 |
-1 |
111 |
-1 |
112 |
-1 |
113 |
-1 |
114 |
-1 |
115 |
-1 |
116 |
-1 |
117 |
[ stdin ]:3: warning: Invalid start of UTF-8 character |
118 |
[ stdin ]:3: warning: Invalid start of UTF-8 character |
119 |
[ stdin ]:3: warning: Invalid start of UTF-8 character |
120 |
[ stdin ]:3: warning: Invalid UTF-8 continuation byte |
121 |
[ stdin ]:3: warning: Invalid start of UTF-8 character |
122 |
[ stdin ]:3: warning: Invalid start of UTF-8 character |
123 |
[ stdin ]:3: warning: Invalid UTF-8 continuation byte |
124 |
[ stdin ]:3: warning: Invalid UTF-8 continuation byte |
125 |
[ stdin ]:3: warning: Invalid start of UTF-8 character |
126 |
[ stdin ]:3: warning: Invalid start of UTF-8 character |
127 |
[ stdin ]:3: warning: Invalid UTF-8 continuation byte |
128 |
[ stdin ]:3: warning: Invalid UTF-8 continuation byte |
129 |
[ stdin ]:3: warning: Invalid UTF-8 continuation byte |
130 |
[ stdin ]:3: warning: Invalid start of UTF-8 character |
131 |
[ stdin ]:3: warning: Invalid start of UTF-8 character |
132 |
## END |
133 |
## BUG bash/zsh stderr-json: "" |
134 |
## BUG bash/zsh STDOUT: |
135 |
1 |
136 |
2 |
137 |
3 |
138 |
4 |
139 |
4 |
140 |
5 |
141 |
6 |
142 |
7 |
143 |
6 |
144 |
7 |
145 |
8 |
146 |
9 |
147 |
10 |
148 |
8 |
149 |
8 |
150 |
## N-I dash stderr-json: "" |
151 |
## N-I dash STDOUT: |
152 |
7 |
153 |
8 |
154 |
9 |
155 |
10 |
156 |
11 |
157 |
12 |
158 |
13 |
159 |
14 |
160 |
15 |
161 |
16 |
162 |
17 |
163 |
18 |
164 |
19 |
165 |
20 |
166 |
20 |
167 |
## END |
168 |
## N-I mksh stderr-json: "" |
169 |
## N-I mksh STDOUT: |
170 |
1 |
171 |
2 |
172 |
3 |
173 |
4 |
174 |
5 |
175 |
6 |
176 |
7 |
177 |
8 |
178 |
9 |
179 |
10 |
180 |
11 |
181 |
12 |
182 |
13 |
183 |
14 |
184 |
14 |
185 |
## END |
186 |
|
187 |
#### Length of undefined variable |
188 |
echo ${#undef} |
189 |
## stdout: 0 |
190 |
|
191 |
#### Length of undefined variable with nounset |
192 |
set -o nounset |
193 |
echo ${#undef} |
194 |
## status: 1 |
195 |
## OK dash status: 2 |
196 |
|