开发者

Why do I get 'use: command not found' when I run my Perl script?

开发者 https://www.devze.com 2022-12-24 08:00 出处:网络
I\'m new to Perl. And I have used following code from one forum to connect to one of the server. but throwing error messages

I'm new to Perl. And I have used following code from one forum to connect to one of the server. but throwing error messages

[root@Cus]# cat tt.pl
#!/usr/bin/perl
use Net::Telnet;
$telnet = new Net::Telnet ( Timeout=>2, Errmode=>'die');
$telnet->open('10.0.0.28');
$telnet->waitfor('/login:/');
$telnet->print('administrator');
$telnet->waitfor('/Password:/');
$telnet->print('test');
$telnet->waitfor('/switch8-12>/');
$telnet->print('whoamI');
$output=$telnet->waitfor('/switch8-12>/');
print $output;

But throwing following error messages.

[root@Cus]# ./tt.pl
./tt.pl: line 3: use: command not found
./tt.pl: line 4: syntax error near unexpected token `('
./tt.pl: line 4: `$telnet = new Net::T开发者_开发问答elnet ( Timeout=>2, Errmode=>'die');'


My guess is that you're using a weird flavour of unix that doesn't respect the #! line, and is trying to run the script via the shell instead of via perl.

Another reason why this might happen is if tt.pl starts with a blank line. The #! must appear at the very start of the file.

Try running perl tt.pl and see what happens.


I had similar error messages and found the reason to be that the perl file was using the wrong character encoding (don't ask me why this mattered to perl). Perl was installed correctly, paths were in order, script syntax was perfect (I even got the "use: command not found" error for a one line "Hello World!" script). Check that tt.pl is UTF8 no-BOM.


As one comment stated "It looks like there is a CR in the beginning".
If you are here hopelessly...
then run d2u script.pl

d2u --> dos to unix.


I don't see these lines at the top of your script, which are essential for all perl modules and scripts:

use strict;
use warnings;

You didn't say which environment this is running in -- are you using bash on linux? Whatever shell you are using does not understand the shebang (#!/usr/bin/perl), and is trying to execute the script in its own language rather than invoking Perl to run it.

Try using /bin/bash, and then your shebang line will work. Or, simply invoke perl explicitly: perl tt.pl.


Use #!/usr/bin/env perl at top of your script.


The same happens in case if the /usr/bin/perl file is empty


I had the same issue and found that there was no perl at #!/usr/bin/perl. neither perl -v or /usr/bin/perl -v were working.

I did a find / -name for perl then added a symbolic link from /usr/bin to the real location

perl -v -bash: perl: command not found /usr/bin/perl -v -bash: /usr/bin/perl: No such file or directory

find / -name perl /opt/ISS/bin/perl5/perl /opt/ISS/bin/perl

ln -s /opt/ISS/bin/perl /usr/bin/perl perl -v

This is perl 5, version 14, subversion 1 (v5.14.1) built for x86_64-linux

Copyright 1987-2011, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page.

/usr/bin/perl -v

This is perl 5, version 14, subversion 1 (v5.14.1) built for x86_64-linux

Copyright 1987-2011, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page.

0

精彩评论

暂无评论...
验证码 换一张
取 消