Question

I write my Perl code in Textpad (which I believe is only avai in Windows). I run it on Linux cmd prompt by calling the Perl interpreter explicitly, e.g. "perl script.pl". I was wondering if it's possible to run it simply as in "./script.pl". When I add the shebang in Windows, the Linux prompt complains "command not found", but it works fine if I call it with Perl, and also works fine after I dos2unix the script, so the issue seems to be the shebang not being parsed correctly. Any suggestions? Why does the rest of the Windows-formatted code work but not the shebang?

Was it helpful?

Solution

Your problem is that Windows prefers a different line ending convention (CRLF, or \r\n) than other operating systems (LF, or \n). Your editor is creating files with \r\n line endings by default.

The shebang is parsed by the operating system, which is not as forgiving as Perl about the stray \r at the end of the command. It tries to run /usr/bin/perl\r, which doesn't exist.

Your text editor should be able to save the script with Unix line-endings. This won't cause problems with using it on Windows, though a few Windows text editors (including Notepad) won't recognize the line endings properly. This will make it work properly on Linux.

OTHER TIPS

The first line of your file is

#!/usr/bin/perl<CR><LF>

<LF> is the line terminator, so the OS tries to launch /usr/bin/perl<CR>. There is no such program. dos2unix changes the first line to

#!/usr/bin/perl<LF>

<LF> is the line terminator, so the OS tries to launch /usr/bin/perl and succeeds.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top