Linux script with bad interpreter: No such file or directory error and fix

codeI had written a script that worked on one machine but not another Linux installation, Red Hat Enterprise Linux 5. The script returned an error with “bad interpreter: No such file or directory” and referenced the first line of code. I could not visually see the error as it looked to be like any other script. The problem turns out to be Micosoft.

I created the script on a Windows box using Notepad++ (not notepad). The script looked correct and I uploaded it to my FTP server then WGET from the Linux box. I then followed all the steps necessary to create a service and when I executed the script it failed with the above mentioned error. After reading through a forum, it turns out that the script had CRLF (Carriage-Return, Line-Feed) at the end of the line. On Linux installations, it is LF (Line-Feed). The resulting script contained an extra CR on the end.

The following perl script was offered and after executing it against the offending script, the file size was noticeably different, although everything visually looked the same, the service (script) executed flawlessly.


die "Usage: $0 < files >\n" unless @ARGV;

for $file (@ARGV)
open IN, $file or die "$0: Cannot open $file for input!\n";

my @lines = <in>;</in>

close IN;
open OUT, "&gt; $file" or die "$0: Cannot open $file for output!\n";

s/\r$// for @lines;
print OUT for @lines;