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

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