
On 23 April 2017 at 15:35, Andrew McGlashan via luv-main <luv-main@luv.asn.au> 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: On 23 April 2017 at 14:14, David <bouncingcats@gmail.com> wrote:
But, doesn't the below script exhibit exactly the behaviour you wanted?
set -eu i=0 for a in a b c do echo "${a}" ((++i)) done
I am writing again because I can't see anywhere you have responded to this suggestion. I get the impression you insist on using the post-increment "i++". Is that because it is more familiar from other languages? The problem is that in bash, this means "check the exitstatus and then increment i". That's not what you need, so why do it? If you use the pre-increment operator, "++i", it increments i and then checks the exitstatus. Which is exactly what you need and want in your original script. It is the simple and appropriate solution. $ i=0; ((i++)); echo "i=$i exitstatus=$?" i=1 exitstatus=1 $ i=0; ((++i)); echo "i=$i exitstatus=$?" i=1 exitstatus=0 Unless I am missing something? :) The 'for' loop solution you showed is fine if you want to do it that way, but it does not solve the original issue you put, which did not use any array. It does let you use "i++" though :)