How are you calling the script - "/path/to/script" or "/bin/sh /path/to/script"?
I'm presuming some formatting was lost in your code as well.
Also, check permissions/ownership... I don't know the UID that runs the script.
Maybe pipe the output from the post-processed command to your log as well, so you can see any errors?