OILS / spec / dparen.test.sh View on Github | oilshell.org

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