It's difficult to give advices without knowing your script. DdlV told you why your code above won't work, but we can't know what other crashes are in the rest of your script.

There are many, many ways to write code that crashes. A crash is often obvious, but if you can't immediately see it in the script, comment out parts until you found the line that crashed. Then examine the line and its context - the most frequent crash reason is an empty pointer.