If you are experiencing a segmentation fault, the error is not in your script; there is a memory management bug in one of your tools (shell, application, c library, or kernel). This does not preclude an actual error in your script, but any such error is not the source of a segmentation fault.
Regards,
Alister
P.S. Please use code tags for code and data. Welcome to the forum.