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 |
#### Can't make assign whole array to var (( b = a )) |
156 |
a=(4 5 6) |
157 |
(( b = a )) |
158 |
echo "${a[@]}" |
159 |
echo "${b[@]}" |
160 |
## status: 1 |
161 |
## stdout-json: "" |
162 |
## BUG bash/mksh status: 0 |
163 |
## BUG bash/mksh STDOUT: |
164 |
4 5 6 |
165 |
4 |
166 |
## END |
167 |
## BUG zsh status: 0 |
168 |
## BUG zsh STDOUT: |
169 |
4 5 6 |
170 |
|
171 |
## END |
172 |
|
173 |
#### set associative array |
174 |
declare -A A=(['foo']=bar ['spam']=42) |
175 |
(( x = A['spam'] )) |
176 |
echo $x |
177 |
## STDOUT: |
178 |
42 |
179 |
## END |
180 |
## N-I mksh status: 1 |
181 |
## N-I mksh stdout-json: "" |
182 |
## N-I zsh STDOUT: |
183 |
0 |
184 |
## END |
185 |
|
186 |
#### Example of incrementing associative array entry with var key (ble.sh) |
187 |
declare -A A=(['foo']=42) |
188 |
key='foo' |
189 |
|
190 |
# note: in bash, (( A[\$key] += 1 )) works the same way. |
191 |
|
192 |
set -- 1 2 |
193 |
(( A[$key] += $2 )) |
194 |
|
195 |
echo foo=${A['foo']} |
196 |
|
197 |
## STDOUT: |
198 |
foo=44 |
199 |
## END |
200 |
## N-I mksh/zsh status: 1 |
201 |
## N-I mksh/zsh stdout-json: "" |
202 |
|