Strange.
I ran the script from the server console and it seems to be working fine. So it means there is something in that script which won't work properly when run from a mac terminal.app, very very strange. This is the first time ever i have come across an app which won't run on shell over ssh like this just because of a terminal running via mac.