/
assign-extended.test.sh
285 lines (246 loc) · 4.23 KB
/
assign-extended.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
#!/bin/bash
#
# Extended assignment language, e.g. typeset, declare, arrays, etc.
# Things that dash doesn't support.
#### local -a
# nixpkgs setup.sh uses this (issue #26)
f() {
local -a array=(x y z)
argv.py "${array[@]}"
}
f
## stdout: ['x', 'y', 'z']
## N-I mksh stdout-json: ""
## N-I mksh status: 1
#### declare -a
# nixpkgs setup.sh uses this (issue #26)
declare -a array=(x y z)
argv.py "${array[@]}"
## stdout: ['x', 'y', 'z']
## N-I mksh stdout-json: ""
## N-I mksh status: 1
#### typeset -a a[1]=a a[3]=c
# declare works the same way in bash, but not mksh.
# spaces are NOT allowed here.
typeset -a a[1*1]=x a[1+2]=z
argv.py "${a[@]}"
## stdout: ['x', 'z']
#### indexed LHS without spaces is allowed
a[1 * 1]=x a[ 1 + 2 ]=z
argv.py "${a[@]}"
## stdout: ['x', 'z']
#### declare -f exit code indicates function existence
func2=x # var names are NOT found
declare -f myfunc func2
echo $?
myfunc() { echo myfunc; }
# This prints the source code.
declare -f myfunc func2 > /dev/null
echo $?
func2() { echo func2; }
declare -f myfunc func2 > /dev/null
echo $?
## STDOUT:
1
1
0
## END
## N-I mksh STDOUT:
127
127
127
## END
#### declare -F prints function names
add () { expr 4 + 4; }
div () { expr 6 / 2; }
ek () { echo hello; }
__ec () { echo hi; }
_ab () { expr 10 % 3; }
declare -F
## STDOUT:
declare -f __ec
declare -f _ab
declare -f add
declare -f div
declare -f ek
## END
## N-I mksh stdout-json: ""
## N-I mksh status: 127
#### declare -p
var1() { echo func; } # function names are NOT found.
declare -p var1 var2 >/dev/null
echo $?
var1=x
declare -p var1 var2 >/dev/null
echo $?
var2=y
declare -p var1 var2 >/dev/null
echo $?
## STDOUT:
1
1
0
## N-I mksh STDOUT:
127
127
127
## END
#### typeset -f
# mksh implement typeset but not declare
typeset -f myfunc func2
echo $?
myfunc() { echo myfunc; }
# This prints the source code.
typeset -f myfunc func2 > /dev/null
echo $?
func2() { echo func2; }
typeset -f myfunc func2 > /dev/null
echo $?
## STDOUT:
1
1
0
## END
#### typeset -p
var1() { echo func; } # function names are NOT found.
typeset -p var1 var2 >/dev/null
echo $?
var1=x
typeset -p var1 var2 >/dev/null
echo $?
var2=y
typeset -p var1 var2 >/dev/null
echo $?
## STDOUT:
1
1
0
## BUG mksh STDOUT:
# mksh doesn't respect exit codes
0
0
0
## END
#### typeset -r makes a string readonly
typeset -r s1='12'
typeset -r s2='34'
s1='c'
echo status=$?
s2='d'
echo status=$?
s1+='e'
echo status=$?
s2+='f'
echo status=$?
unset s1
echo status=$?
unset s2
echo status=$?
## status: 1
## stdout-json: ""
## OK mksh status: 2
## OK bash status: 0
## OK bash STDOUT:
status=1
status=1
status=1
status=1
status=1
status=1
## END
#### typeset -ar makes it readonly
typeset -a -r array1=(1 2)
typeset -ar array2=(3 4)
array1=('c')
echo status=$?
array2=('d')
echo status=$?
array1+=('e')
echo status=$?
array2+=('f')
echo status=$?
unset array1
echo status=$?
unset array2
echo status=$?
## status: 1
## stdout-json: ""
## OK bash status: 0
## OK bash STDOUT:
status=1
status=1
status=1
status=1
status=1
status=1
## END
## N-I mksh status: 1
## N-I mksh stdout-json: ""
#### typeset -x makes it exported
typeset -rx PYTHONPATH=lib/
printenv.py PYTHONPATH
## STDOUT:
lib/
## END
#### Multiple assignments / array assignments on a line
a=1 b[0+0]=2 c=3
echo $a ${b[@]} $c
## stdout: 1 2 3
#### Env bindings shouldn't contain array assignments
a=1 b[0]=2 c=3 printenv.py a b c
## status: 2
## stdout-json: ""
## OK bash STDOUT:
1
None
3
## END
## OK bash status: 0
## BUG mksh STDOUT:
1
2
3
## END
## OK mksh status: 0
#### syntax error in array assignment
a=x b[0+]=y c=z
echo $a $b $c
## status: 2
## stdout-json: ""
## BUG bash stdout: x
## BUG bash status: 0
## OK mksh stdout-json: ""
## OK mksh status: 1
#### declare -g (bash-specific; bash-completion uses it)
f() {
declare -g G=42
declare L=99
declare -Ag dict
dict["foo"]=bar
declare -A localdict
localdict["spam"]=Eggs
# For bash-completion
eval 'declare -Ag ev'
ev["ev1"]=ev2
}
f
argv.py "$G" "$L"
argv.py "${dict["foo"]}" "${localdict["spam"]}"
argv.py "${ev["ev1"]}"
## STDOUT:
['42', '']
['bar', '']
['ev2']
## END
## N-I mksh STDOUT:
['', '']
## END
## N-I mksh status: 1
#### myvar=typeset (another form of dynamic assignment)
myvar=typeset
x='a b'
$myvar x=$x
echo $x
## STDOUT:
a
## END