Hi, On 24/04/17 10:24, David via luv-main wrote: > On 23 April 2017 at 15:35, Andrew McGlashan via luv-main > <[email protected]> wrote: >> Hi, >> >> On 23/04/17 14:42, David via luv-main wrote: >>> If you insist on using 'set -e', you could do this to disable >>> exitstatus checking on arithmetic contexts: >>> >>> set -eu >>> i=0 >>> for a in a b c >>> do >>> echo "${a}" >>> ((i++)) || true >>> done >> >> Yes, that would be the cleanest way to solve this. Thanks. > > I disagree, I think it is a bit hackish, but it is a universal > solution to the collision of trying to use 'set -e' together with > unknown arithmetic results that might include zero, so I thought > it was worth adding to the discussion. > > But in your example use-case, the arithmetic results are entirely predictable. > > In the message I sent before the one above, I gave what seemed to me > the cleanest way to do what you originally asked, using pre-increment > as I suggested in my first reply:
Yes, I did see that and I tried it out; pre-increment gives problems
just the same as post-increment. So that wasn't a solution.
There is obvious debate on both sides and I can understand the various
arguments for both points -- using set -x or not AND using the ((i++))
construct or not,
To me, arithmetic math with the use of the double round brackets is, due
to the other use of the construct, a mistake to have been implemented
that way.
Using "((i++)) || true" allows me to say, I don't care if the arithmetic
result is zero, I am handling the condition for loop exit outside of
that result and the actual return from the ((i++)) is totally irrelevant.
The other alternative that was good was this one:
for ((i=0; i<${#array[@]}, i++))
do
...
done
Anyway, I am using the C construct with the for loop now as it is
slightly better IMHO. But I would have no problem using the other
solution of:
((i++)) || true
- when it suits...
Thanks and Cheers
AndrewM
signature.asc
Description: OpenPGP digital signature
_______________________________________________ luv-main mailing list [email protected] https://lists.luv.asn.au/cgi-bin/mailman/listinfo/luv-main
