DOS Line endings break CGI script execution
Some fellows in another office asked for help figuring out why the CGI script they were writing wouldn’t run.
Yesterday, I’d helped them find that the interpreter named on the she-bang line (#!) of their scripts didn’t exist and I’d recommended they use “#!/usr/bin/env perl” instead. They did, but wrote the test.cgi script in notepad and then transfered the file to the Linux web server with something that left the DOS line endings intact.
So, when Apache tried to run the script, the /usr/bin/env program got handed “perl^M” instead of “perl,” couldn’t find that in the path anywhere and blew chunks. Here’s what I wrote back to explain:
The problem exists when you use /usr/bin/env to invoke the interpreter and pass it a string (the interpreter name) on the command line and that string ends in a trailing ^M. That ^M is the part of the DOS line ending that dos2unix would remove. So, in your simpletest.cgi script, this is what Unix is seeing as the top line:
#!/usr/bin/env perl^M
#!/usr/bin/env perl -w ^M