Some debugging effort from your side may help you know.
You say that condition causes it.
Well, split it up over more lines.
When needed, use temp variables to store intermediate results.
Then try again.
- If it still stops execution unexpectedly, start inserting echoes in the lines until you found the single command or identifier causing it.
- If the problem is gone, start combining again, step by step, until it happens again, then the last combining step was the reason.