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
|
12 |
if (( 3 > 2)); then
|
13 |
echo True
|
14 |
fi
|
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 ))
|
21 |
echo $x $y
|
22 |
## stdout: 1 3
|
23 |
|
24 |
#### (( )) with arrays
|
25 |
a=(4 5 6)
|
26 |
(( sum = a[0] + a[1] + a[2] ))
|
27 |
echo $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
|
35 |
echo $((a + b + c))
|
36 |
## STDOUT:
|
37 |
false
|
38 |
true
|
39 |
true
|
40 |
0
|
41 |
## END
|
42 |
|
43 |
|
44 |
#### bash and mksh: V in (( a[K] = V )) gets coerced to integer
|
45 |
shopt -u strict_arith || true
|
46 |
K=key
|
47 |
V=value
|
48 |
typeset -a a
|
49 |
(( a[K] = V ))
|
50 |
|
51 |
# not there!
|
52 |
echo a[\"key\"]=${a[$K]}
|
53 |
|
54 |
echo keys = ${!a[@]}
|
55 |
echo values = ${a[@]}
|
56 |
## STDOUT:
|
57 |
a["key"]=0
|
58 |
keys = 0
|
59 |
values = 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
|
65 |
K=5
|
66 |
V=42
|
67 |
typeset -A A
|
68 |
(( A[K] = V ))
|
69 |
|
70 |
echo A["5"]=${A["5"]}
|
71 |
echo keys = ${!A[@]}
|
72 |
echo values = ${A[@]}
|
73 |
## STDOUT:
|
74 |
A[5]=
|
75 |
keys = K
|
76 |
values = 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
|
86 |
typeset -A A
|
87 |
K=5
|
88 |
V=42
|
89 |
A["$K"]=$V
|
90 |
A["K"]=oops
|
91 |
A[K]=oops2
|
92 |
|
93 |
# We don't neither 42 nor "oops". Bad!
|
94 |
(( V = A[K] ))
|
95 |
|
96 |
echo V=$V
|
97 |
## status: 1
|
98 |
## stdout-json: ""
|
99 |
## BUG bash/zsh status: 0
|
100 |
## BUG bash/zsh STDOUT:
|
101 |
V=0
|
102 |
## END
|
103 |
|
104 |
#### bash: V in (( A["K"] = V )) gets coerced to integer
|
105 |
shopt -u strict_arith || true
|
106 |
K=key
|
107 |
V=value
|
108 |
typeset -A A || exit 1
|
109 |
(( A["K"] = V ))
|
110 |
|
111 |
# not there!
|
112 |
echo A[\"key\"]=${A[$K]}
|
113 |
|
114 |
echo keys = ${!A[@]}
|
115 |
echo values = ${A[@]}
|
116 |
## STDOUT:
|
117 |
A["key"]=
|
118 |
keys = K
|
119 |
values = 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 (( ))
|
127 |
declare -A A
|
128 |
A['x']=42
|
129 |
(( x = A['x'] ))
|
130 |
(( A['y'] = 'y' )) # y is a variable, gets coerced to 0
|
131 |
echo $x ${A['y']}
|
132 |
## STDOUT:
|
133 |
42 0
|
134 |
## END
|
135 |
## BUG mksh status: 0
|
136 |
## N-I mksh STDOUT:
|
137 |
0
|
138 |
## END
|
139 |
## BUG zsh status: 0
|
140 |
## N-I zsh STDOUT:
|
141 |
42
|
142 |
## END
|
143 |
|
144 |
#### (( )) with redirect
|
145 |
(( a = $(stdout_stderr.py 42) + 10 )) 2>$TMP/x.txt
|
146 |
echo $a
|
147 |
echo --
|
148 |
cat $TMP/x.txt
|
149 |
## STDOUT:
|
150 |
52
|
151 |
--
|
152 |
STDERR
|
153 |
## END
|
154 |
|
155 |
#### Assigning whole raray (( b = a ))
|
156 |
a=(4 5 6)
|
157 |
(( b = a ))
|
158 |
|
159 |
echo "${a[@]}"
|
160 |
|
161 |
# OSH doesn't like this
|
162 |
echo "${b[@]}"
|
163 |
|
164 |
## status: 0
|
165 |
## STDOUT:
|
166 |
4 5 6
|
167 |
4
|
168 |
## END
|
169 |
## BUG zsh status: 0
|
170 |
## BUG zsh STDOUT:
|
171 |
4 5 6
|
172 |
|
173 |
## END
|
174 |
|
175 |
#### set associative array
|
176 |
declare -A A=(['foo']=bar ['spam']=42)
|
177 |
(( x = A['spam'] ))
|
178 |
echo $x
|
179 |
## STDOUT:
|
180 |
42
|
181 |
## END
|
182 |
## N-I mksh status: 1
|
183 |
## N-I mksh stdout-json: ""
|
184 |
## N-I zsh STDOUT:
|
185 |
0
|
186 |
## END
|
187 |
|
188 |
#### Example of incrementing associative array entry with var key (ble.sh)
|
189 |
declare -A A=(['foo']=42)
|
190 |
key='foo'
|
191 |
|
192 |
# note: in bash, (( A[\$key] += 1 )) works the same way.
|
193 |
|
194 |
set -- 1 2
|
195 |
(( A[$key] += $2 ))
|
196 |
|
197 |
echo foo=${A['foo']}
|
198 |
|
199 |
## STDOUT:
|
200 |
foo=44
|
201 |
## END
|
202 |
## N-I mksh/zsh status: 1
|
203 |
## N-I mksh/zsh stdout-json: ""
|
204 |
|