Are you sure that your script file doesn't contain ^M character(s) ?
Code:
>cat -v ctrlM.sh
#/usr/bin/bash
# Next line is ctrl-M
^M
date
>sh -vx ctrlM.sh
#/usr/bin/bash
# Next line is ctrl-M
+ $'\r'
ctrlM.sh: line 4: $'\r': command not found
date
+ date
Sun Jul 27 22:34:29 2008
>
Try to remove ^M:
Code:
>tr -d '\r' <ctrlM.sh >no_ctrlM.sh>sh -vx no_ctrlM.sh
#/usr/bin/bash
# Next line is ctrl-M
date
+ date
Sun Jul 27 22:37:49 2008
>
Windows editors produce files that have two characters that define the end of a line - so-called carriage control.
UNIX just uses one character, so files from DOS or Windows show an extra character ascii 13 or control-M == ^M
UNIX shell does not like the ^M character. Delete all of the ^M characters and things will be fine. You can do it with Aigles tr example or with dos2ux (sometimes called dos2unix).
How to get rid of the ^M characters
Code:
# way number 1
dos2ux anticrash.sh > tmfile; mv tmfile anticrash.sh; chmod +x anticrash.sh
#way number 2
tr -d '\r' < aniticrash.sh > newanticrash.sh; chmod +x newanticrash.sh
# note '\r' is the return button or ^M