/
array.test.sh
510 lines (440 loc) · 9.59 KB
/
array.test.sh
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#!/usr/bin/env bash
# TODO: Need a SETUP section.
#### SETUP
a=(1 '2 3')
#### "${a[@]}" and "${a[*]}"
a=(1 '2 3')
argv.py "${a[@]}" "${a[*]}"
## stdout: ['1', '2 3', '1 2 3']
#### ${a[@]} and ${a[*]}
a=(1 '2 3')
argv.py ${a[@]} ${a[*]}
## stdout: ['1', '2', '3', '1', '2', '3']
#### 4 ways to interpolate empty array
argv.py 1 "${a[@]}" 2 ${a[@]} 3 "${a[*]}" 4 ${a[*]} 5
## stdout: ['1', '2', '3', '', '4', '5']
#### empty array
empty=()
argv.py "${empty[@]}"
## stdout: []
#### Empty array with :-
empty=()
argv.py ${empty[@]:-not one} "${empty[@]:-not one}"
## stdout: ['not', 'one', 'not one']
#### nounset with empty array (design bug, makes it hard to use arrays)
# http://lists.gnu.org/archive/html/help-bash/2017-09/msg00005.html
# NOTE: This used to be a bug in bash 4.3, but is fixed in bash 4.4.
set -o nounset
empty=()
argv.py "${empty[@]}"
echo status=$?
## STDOUT:
[]
status=0
## END
## BUG mksh stdout-json: ""
## BUG mksh status: 1
#### local array
# mksh support local variables, but not local arrays, oddly.
f() {
local a=(1 '2 3')
argv.py "${a[0]}"
}
f
## stdout: ['1']
## status: 0
## BUG mksh status: 1
## BUG mksh stdout-json: ""
#### Command with with word splitting in array
array=('1 2' $(echo '3 4'))
argv.py "${array[@]}"
## stdout: ['1 2', '3', '4']
#### space before ( in array initialization
# NOTE: mksh accepts this, but bash doesn't
a= (1 '2 3')
echo $a
## status: 2
## OK mksh status: 0
## OK mksh stdout: 1
#### array over multiple lines
a=(
1
'2 3'
)
argv.py "${a[@]}"
## stdout: ['1', '2 3']
## status: 0
#### array with invalid token
a=(
1
&
'2 3'
)
argv.py "${a[@]}"
## status: 2
## OK mksh status: 1
#### array with empty string
empty=('')
argv.py "${empty[@]}"
## stdout: ['']
#### Retrieve index
a=(1 '2 3')
argv.py "${a[1]}"
## stdout: ['2 3']
#### Retrieve out of bounds index
a=(1 '2 3')
argv.py "${a[3]}"
## stdout: ['']
#### Negative index
a=(1 '2 3')
argv.py "${a[-1]}" "${a[-2]}" "${a[-5]}" # last one out of bounds
## stdout: ['2 3', '1', '']
## N-I mksh stdout: ['', '', '']
#### Retrieve index that is a variable
a=(1 '2 3')
i=1
argv.py "${a[$i]}"
## stdout: ['2 3']
#### Retrieve index that is a variable without $
a=(1 '2 3')
i=5
argv.py "${a[i-4]}"
## stdout: ['2 3']
#### Retrieve index that is a command sub
a=(1 '2 3')
argv.py "${a[$(echo 1)]}"
## stdout: ['2 3']
#### Retrieve array indices with ${!a}
a=(1 '2 3')
argv.py "${!a[@]}"
## stdout: ['0', '1']
#### Retrieve sparse array indices with ${!a}
a=()
(( a[99]=1 ))
argv.py "${!a[@]}"
## STDOUT:
['99']
## END
#### ${!a[1]} is named ref in bash
# mksh ignores it
foo=bar
a=('1 2' foo '2 3')
argv.py "${!a[1]}"
## status: 0
## stdout: ['bar']
## N-I mksh stdout: ['a[1]']
#### ${!a} on array is disallowed
# bash gives empty string because it's like a[0]
# mksh gives the name of the variable with !. Very weird.
a=(1 '2 3')
argv.py "${!a}"
## stdout-json: ""
## status: 1
## BUG bash stdout: ['']
## BUG bash status: 0
## BUG mksh stdout: ['a']
## BUG mksh status: 0
#### All elements unquoted
a=(1 '2 3')
argv.py ${a[@]}
## stdout: ['1', '2', '3']
#### All elements quoted
a=(1 '2 3')
argv.py "${a[@]}"
## stdout: ['1', '2 3']
#### $*
a=(1 '2 3')
argv.py ${a[*]}
## stdout: ['1', '2', '3']
#### "$*"
a=(1 '2 3')
argv.py "${a[*]}"
## stdout: ['1 2 3']
#### Interpolate array into array
a=(1 '2 3')
a=(0 "${a[@]}" '4 5')
argv.py "${a[@]}"
## stdout: ['0', '1', '2 3', '4 5']
#### Exporting array doesn't do anything, not even first element
# bash parses, but doesn't execute.
# mksh gives syntax error -- parses differently with 'export'
# osh no longer parses this statically.
export PYTHONPATH=(a b c)
export PYTHONPATH=a # NOTE: in bash, this doesn't work afterward!
printenv.py PYTHONPATH
## stdout: None
## OK mksh stdout-json: ""
## OK mksh status: 1
## OK osh stdout-json: ""
## OK osh status: 2
#### Arrays can't be used as env bindings
# Hm bash it treats it as a string!
A=a B=(b b) printenv.py A B
## status: 2
## stdout-json: ""
## OK bash stdout-json: "a\n(b b)\n"
## OK bash status: 0
## OK mksh status: 1
#### Set element
a=(1 '2 3')
a[0]=9
argv.py "${a[@]}"
## stdout: ['9', '2 3']
#### Set element with var ref
a=(1 '2 3')
i=0
a[$i]=9
argv.py "${a[@]}"
## stdout: ['9', '2 3']
#### Set element with array ref
# This makes parsing a little more complex. Anything can be inside [],
# including other [].
a=(1 '2 3')
i=(0 1)
a[${i[1]}]=9
argv.py "${a[@]}"
## stdout: ['1', '9']
#### Set array item to array
a=(1 2)
a[0]=(3 4)
echo "status=$?"
## stdout: status=1
## status: 0
## N-I mksh stdout-json: ""
## N-I mksh status: 1
#### Slice of array with [@]
# mksh doesn't support this syntax! It's a bash extension.
a=(1 2 3)
argv.py "${a[@]:1:2}"
## stdout: ['2', '3']
## N-I mksh status: 1
## N-I mksh stdout-json: ""
#### Negative slice begin
# mksh doesn't support this syntax! It's a bash extension.
# NOTE: for some reason -2) has to be in parens? Ah that's because it
# conflicts with :-! That's silly. You can also add a space.
a=(1 2 3 4 5)
argv.py "${a[@]:(-4)}"
## stdout: ['2', '3', '4', '5']
## N-I mksh status: 1
## N-I mksh stdout-json: ""
#### Negative slice length
a=(1 2 3 4 5)
argv.py "${a[@]: 1: -3}"
## status: 1
## stdout-json: ""
#### Slice with arithmetic
a=(1 2 3)
i=5
argv.py "${a[@]:i-4:2}"
## stdout: ['2', '3']
## N-I mksh status: 1
## N-I mksh stdout-json: ""
#### Number of elements
a=(1 '2 3')
echo "${#a[@]}" ${#a[@]} # bug fix: also test without quotes
## stdout: 2 2
#### Length of an element
a=(1 '2 3')
echo "${#a[1]}"
## stdout: 3
#### Iteration
a=(1 '2 3')
for v in "${a[@]}"; do
echo $v
done
## stdout-json: "1\n2 3\n"
#### glob within array yields separate elements
touch _tmp/y.Y _tmp/yy.Y
a=(_tmp/*.Y)
argv.py "${a[@]}"
## stdout: ['_tmp/y.Y', '_tmp/yy.Y']
#### declare array and then append
declare -a array
array+=(a)
array+=(b c)
argv.py "${array[@]}"
## stdout: ['a', 'b', 'c']
#### Array syntax in wrong place
ls foo=(1 2)
## status: 2
## OK mksh status: 1
#### Single array with :-
# bash does EMPTY ELISION here, unless it's double quoted. mksh has
# more sane behavior. OSH is better.
single=('')
argv.py ${single[@]:-none} x "${single[@]:-none}"
## OK osh stdout: ['x', '']
## OK bash stdout: ['none', 'x', '']
## OK mksh stdout: ['none', 'x', 'none']
#### Stripping a whole array unquoted
# Problem: it joins it first.
files=('foo.c' 'sp ace.h' 'bar.c')
argv.py ${files[@]%.c}
## status: 0
## stdout: ['foo', 'sp', 'ace.h', 'bar']
## N-I mksh status: 1
## N-I mksh stdout-json: ""
#### Stripping a whole array quoted
files=('foo.c' 'sp ace.h' 'bar.c')
argv.py "${files[@]%.c}"
## status: 0
## stdout: ['foo', 'sp ace.h', 'bar']
## N-I mksh status: 1
## N-I mksh stdout-json: ""
#### Multiple subscripts not allowed
# NOTE: bash 4.3 had a bug where it ignored the bad subscript, but now it is
# fixed.
a=('123' '456')
argv.py "${a[0]}" "${a[0][0]}"
## stdout-json: ""
## status: 2
## OK bash/mksh status: 1
#### Length op, index op, then transform op is not allowed
a=('123' '456')
echo "${#a[0]}" "${#a[0]/1/xxx}"
## stdout-json: ""
## status: 2
## OK bash/mksh status: 1
#### Array subscript not allowed on string
s='abc'
echo ${s[@]}
## BUG bash/mksh status: 0
## BUG bash/mksh stdout: abc
## status: 1
#### Create a "user" array out of the argv array
set -- 'a b' 'c'
array1=('x y' 'z')
array2=("$@")
argv.py "${array1[@]}" "${array2[@]}"
## stdout: ['x y', 'z', 'a b', 'c']
#### Tilde expansion within array
HOME=/home/bob
a=(~/src ~/git)
echo "${a[@]}"
## stdout: /home/bob/src /home/bob/git
#### Brace Expansion within Array
a=(-{a,b} {c,d}-)
echo "${a[@]}"
## stdout: -a -b c- d-
#### array default
default=('1 2' '3')
argv.py "${undef[@]:-${default[@]}}"
## stdout: ['1 2', '3']
#### Singleton Array Copy and Assign. OSH can't index strings with ints
a=( '12 3' )
b=( "${a[@]}" )
c="${a[@]}" # This decays it to a string
d=${a[*]} # This decays it to a string
echo ${#a[0]} ${#b[0]}
echo ${#a[@]} ${#b[@]}
# osh is intentionally stricter, and these fail.
echo ${#c[0]} ${#d[0]}
echo ${#c[@]} ${#d[@]}
## status: 1
## STDOUT:
4 4
1 1
## END
## OK bash/mksh status: 0
## OK bash/mksh STDOUT:
4 4
1 1
4 4
1 1
## END
#### declare -a / local -a is empty array
declare -a myarray
argv.py "${myarray[@]}"
myarray+=('x')
argv.py "${myarray[@]}"
f() {
local -a myarray
argv.py "${myarray[@]}"
myarray+=('x')
argv.py "${myarray[@]}"
}
f
## STDOUT:
[]
['x']
[]
['x']
## END
#### Create sparse array
a=()
(( a[99]=1 )) # osh doesn't parse index assignment outside arithmetic yet
echo len=${#a[@]}
argv.py "${a[@]}"
echo "unset=${a[33]}"
echo len-of-unset=${#a[33]}
## STDOUT:
len=1
['1']
unset=
len-of-unset=0
## END
#### Create sparse array implicitly
(( a[99]=1 ))
echo len=${#a[@]}
argv.py "${a[@]}"
echo "unset=${a[33]}"
echo len-of-unset=${#a[33]}
## STDOUT:
len=1
['1']
unset=
len-of-unset=0
## END
#### Append sparse arrays
a=()
(( a[99]=1 ))
b=()
(( b[33]=2 ))
(( b[66]=3 ))
a+=( "${b[@]}" )
argv.py "${a[@]}"
argv.py "${a[99]}" "${a[100]}" "${a[101]}"
## STDOUT:
['1', '2', '3']
['1', '2', '3']
## END
#### Slice of sparse array with [@]
# mksh doesn't support this syntax! It's a bash extension.
(( a[33]=1 ))
(( a[66]=2 ))
(( a[99]=2 ))
argv.py "${a[@]:15:2}"
## stdout: ['1', '2']
## N-I mksh status: 1
## N-I mksh stdout-json: ""
#### Using an array itself as the index on LHS
# TODO: Fix OSH crash.
shopt -u strict-arith
a[a]=42
a[a]=99
argv "${a[@]}" "${a[0]}" "${a[42]}" "${a[99]}"
## STDOUT:
['42', '99', '42', '99', '']
## END
#### Using an array itself as the index on RHS
shopt -u strict-arith
a=(1 2 3)
(( x = a[a] ))
echo $x
## stdout-json: ""
## BUG bash/mksh STDOUT:
2
## END
#### a[$x$y] on LHS and RHS
x=1
y=2
a[$x$y]=foo
# not allowed by OSH parsing
#echo ${a[$x$y]}
echo ${a[12]}
echo ${#a[@]}
## STDOUT:
foo
1
## END