If you receive the message "bad interpreter: No such file or directory" when attempting to execute a Perl script, it may because the path to Perl is incorrect in the script. You can check the location of the Perl executable with
which perl. Some common locations for Perl
are shown below:
/uisr/bin/perl
/usr/sbin/perl
/usr/local/bin/perl
/usr/bin/perl5
/usr/local/bin/perl5
The first line of the Perl script should point to the location revealed
for Perl by which perl. E.g., if Perl is in /usr/bin, you should
see the following line as the first line of the Perl script.
#!/usr/bin/perl
But, if the path is listed correctly in the script, another check to make is that you aren't using a Perl file in DOS format. For instance, if the file was created on a Windows system and was transferred to a Unix or Linux system in binary mode rather than ASCII mode, then the lines in the file may not be terminated properly for the Linux or Unix system. For a Linux or Unix system, each line should be terminated with a newline character, i.e. hexadecimal 0A. A DOS formatted file, i.e. the text file type you would find on a Windows system will use both a carriage return (CR) and line feed (LF), i.e. a hexadecimal 0D followed by a hexadecimal 0A.
If you attempt to run a Perl script which uses the DOS format on a Unix or Linux system, you will likely get the error message ": bad interpreter: No such file or directory"
You can check the line endins with the hexdump command. Below are two example files test.pl and test2.pl, which are identical, except for the line endings.
# hexdump -C test.pl
00000000 23 21 2f 75 73 72 2f 62 69 6e 2f 70 65 72 6c 0d |#!/usr/bin/perl.|
00000010 0a 0d 0a 70 72 69 6e 74 20 22 68 65 6c 6c 6f 5c |...print "hello\|
00000020 6e 22 3b 0d 0a |n";..|
00000025
# hexdump -C test2.pl
00000000 23 21 2f 75 73 72 2f 62 69 6e 2f 70 65 72 6c 0a |#!/usr/bin/perl.|
00000010 0a 70 72 69 6e 74 20 22 68 65 6c 6c 6f 5c 6e 22 |.print "hello\n"|
00000020 3b 0a 0a |;..|
00000023
If you examined the code in a regular editor, you would see the following lines in each file:
#!/usr/bin/perl
print "hello\n";
But, if you tried to execute them on a Unix or Linux system, you would see different results.
# ./test.pl
: bad interpreter: No such file or directory
# ./test2.pl
hello
You can convert a file, e.g. test.pl, to Unix text file format with dos2unix, which is a DOS/MAC to UNIX text file format converter.
dos2unix test.pl
References:
