1 |
Mike Frysinger wrote: |
2 |
|
3 |
> On Monday 24 September 2007, Donnie Berkholz wrote: |
4 |
>> |
5 |
>> You could use C-style syntax here: |
6 |
>> |
7 |
>> (( use_errors++ )) |
8 |
>> |
9 |
>> I find it a bit more readable. |
10 |
> |
11 |
> i like to get anal and use ((++use_errors)) |
12 |
> |
13 |
> then again, it may also be more readable like so: |
14 |
> use_errors=false |
15 |
> if ... ; then |
16 |
> ... |
17 |
> use_errors=true |
18 |
> fi |
19 |
> if ... ; then |
20 |
> ... |
21 |
> use_errors=true |
22 |
> fi |
23 |
> ${use_errors} && die "..." |
24 |
> -mike |
25 |
|
26 |
Integer arithmetic is generally quicker: |
27 |
((use_errors++)) [or: use_errors=1 ] |
28 |
.. |
29 |
((use_errors)) && die 'meh' |
30 |
-- although true and false are builtins in this case. |
31 |
For stuff like for ((i=0;i<n;i++)) arithmetical context[1] is significantly |
32 |
quicker than for i in {0..15} say, and more flexible (variable bound and |
33 |
step-size, if desired.) |
34 |
|
35 |
The boolean test on an integer eg ((use_errors)) is a metaphor I use a lot, |
36 |
eg: ((quiet)) || echo oops; ((verbose>1)) && echo blah |
37 |
|
38 |
It's handy as unset vars test to false, and also as the usual boolean values |
39 |
apply, ie 0 is false, anything else is true. (Outside this context 0 is |
40 |
true [success from cmd] and anything else is false [error code].) And ofc, |
41 |
you don't need to put $ in front of your standard variables in this |
42 |
context. |
43 |
|
44 |
[1] http://wooledge.org/mywiki/ArithmeticExpression |
45 |
|
46 |
|
47 |
-- |
48 |
gentoo-dev@g.o mailing list |