OILS / spec / dparen.test.sh View on Github | oils.pub

208 lines, 96 significant
1## compare_shells: bash-4.4 mksh zsh
2
3#### (( )) result
4(( 1 )) && echo True
5(( 0 )) || echo False
6## STDOUT:
7True
8False
9## END
10
11#### negative number is true
12(( -1 )) && echo True
13## stdout: True
14
15#### (( )) in if statement
16if (( 3 > 2)); then
17 echo True
18fi
19## stdout: True
20
21#### (( ))
22# What is the difference with this and let? One difference: spaces are allowed.
23(( x = 1 ))
24(( y = x + 2 ))
25echo $x $y
26## stdout: 1 3
27
28#### (( )) with arrays
29a=(4 5 6)
30(( sum = a[0] + a[1] + a[2] ))
31echo $sum
32## stdout: 15
33## OK zsh stdout: 9
34
35#### (( )) with error
36(( a = 0 )) || echo false
37(( b = 1 )) && echo true
38(( c = -1 )) && echo true
39echo $((a + b + c))
40## STDOUT:
41false
42true
43true
440
45## END
46
47
48#### bash and mksh: V in (( a[K] = V )) gets coerced to integer
49shopt -u strict_arith || true
50K=key
51V=value
52typeset -a a
53(( a[K] = V ))
54
55# not there!
56echo a[\"key\"]=${a[$K]}
57
58echo keys = ${!a[@]}
59echo values = ${a[@]}
60## STDOUT:
61a["key"]=0
62keys = 0
63values = 0
64## END
65## N-I zsh status: 1
66## N-I zsh stdout-json: ""
67
68#### bash: K in (( A[K] = V )) is a constant string
69K=5
70V=42
71typeset -A A
72(( A[K] = V ))
73
74echo A["5"]=${A["5"]}
75echo keys = ${!A[@]}
76echo values = ${A[@]}
77## STDOUT:
78A[5]=
79keys = K
80values = 42
81## END
82## OK osh status: 1
83## OK osh stdout-json: ""
84## N-I zsh status: 1
85## N-I zsh stdout-json: ""
86## N-I mksh status: 1
87## N-I mksh stdout-json: ""
88
89#### BUG: (( V = A[K] )) doesn't retrieve the right value
90typeset -A A
91K=5
92V=42
93A["$K"]=$V
94A["K"]=oops
95A[K]=oops2
96
97# We don't neither 42 nor "oops". Bad!
98(( V = A[K] ))
99
100echo V=$V
101## status: 1
102## stdout-json: ""
103## BUG bash/zsh status: 0
104## BUG bash/zsh STDOUT:
105V=0
106## END
107
108#### bash: V in (( A["K"] = V )) gets coerced to integer
109shopt -u strict_arith || true
110K=key
111V=value
112typeset -A A || exit 1
113(( A["K"] = V ))
114
115# not there!
116echo A[\"key\"]=${A[$K]}
117
118echo keys = ${!A[@]}
119echo values = ${A[@]}
120## STDOUT:
121A["key"]=
122keys = K
123values = 0
124## END
125## N-I zsh stdout-json: ""
126## N-I zsh status: 1
127## N-I mksh stdout-json: ""
128## N-I mksh status: 1
129
130#### literal strings inside (( ))
131declare -A A
132A['x']=42
133(( x = A['x'] ))
134(( A['y'] = 'y' )) # y is a variable, gets coerced to 0
135echo $x ${A['y']}
136## STDOUT:
13742 0
138## END
139## N-I mksh status: 0
140## N-I mksh STDOUT:
1410
142## END
143## N-I zsh status: 0
144## N-I zsh STDOUT:
14542
146## END
147
148#### (( )) with redirect
149(( a = $(stdout_stderr.py 42) + 10 )) 2>$TMP/x.txt
150echo $a
151echo --
152cat $TMP/x.txt
153## STDOUT:
15452
155--
156STDERR
157## END
158
159#### Assigning whole raray (( b = a ))
160a=(4 5 6)
161(( b = a ))
162
163echo "${a[@]}"
164
165# OSH doesn't like this
166echo "${b[@]}"
167
168## status: 0
169## STDOUT:
1704 5 6
1714
172## END
173## BUG zsh status: 0
174## BUG zsh STDOUT:
1754 5 6
176
177## END
178
179#### set associative array
180declare -A A=(['foo']=bar ['spam']=42)
181(( x = A['spam'] ))
182echo $x
183## STDOUT:
18442
185## END
186## N-I mksh status: 1
187## N-I mksh stdout-json: ""
188## N-I zsh STDOUT:
1890
190## END
191
192#### Example of incrementing associative array entry with var key (ble.sh)
193declare -A A=(['foo']=42)
194key='foo'
195
196# note: in bash, (( A[\$key] += 1 )) works the same way.
197
198set -- 1 2
199(( A[$key] += $2 ))
200
201echo foo=${A['foo']}
202
203## STDOUT:
204foo=44
205## END
206## N-I mksh/zsh status: 1
207## N-I mksh/zsh stdout-json: ""
208