So we have all come across the situation: you copy a bunch of files from DOS/Windows to Unix and the newline characters are all messed up (in other words, you see a "^M" at the end of each line). So the scripts you wrote lovingly, are all messed up now.
There are two choices:
Use dos2unix - this will change the newline characters one file at a time. Trouble is, that dos2unix writes to stdout, so you'd have to pipe the output to a temporary file and move it back to your script...
Use the following perl one-liner:
perl -pi -e 's/\r\n/\n/g' *
You guessed it - Windows/DOS uses "\r\n" for newline, while Unix uses only "\n"