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

I am new to Perl. And I used the following code from one forum to connect to one of the servers. but throw 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 throw the 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::Telnet ( Timeout=>2, Errmode=>'die');' 
+4
source share
6 answers

I assume you are using a strange unix flavor that doesn't respect the #! line #! , and tries to run the script through the shell, and not through perl.

Another reason this can happen is if tt.pl starts with an empty string. #! should be displayed at the very beginning of the file.

Try running perl tt.pl and see what happens.

+12
source

I had similar error messages and found the reason that the perl file used the wrong character encoding (don't ask me why this matters perl). Perl was installed correctly, the paths were fine, the script syntax was perfect (I even got a "use: command not found" error for one line of the "Hello World!" Script). Make sure tt.pl is no UTF8 specification.

+4
source

As one comment says: β€œIt looks like there is CR in the beginning.”
If you are hopeless here ...
then run d2u script.pl

d2u dos for unix.

+3
source

I do not see these lines at the top of your script, which are necessary for all perl modules and scripts:

 use strict; use warnings; 

You did not say in what environment this works - do you use bash on linux? No matter which shell you use, it does not understand shebang ( #!/usr/bin/perl ) and tries to execute the script in its own language, instead of referencing Perl to run it.

Try using / bin / bash and then your shebang line will work. Or just call perl explicitly: perl tt.pl

+1
source

The same thing happens if the file /usr/bin/perl empty

0
source

I had the same problem and found that perl was not #! / Usr / bin / perl. neither perl -v nor / usr / bin / perl -v worked.

I did find / -name for perl and then added a symlink 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 / MKS / bin / perl5 / Perl / Opt / MKS / 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 can only be copied under either the Artistic License or the GNU General Public License, which can be found in the original Perl 5 suite.

Full documentation for Perl, including lists of frequently asked questions, should be found on this system using "man perl" or "perldoc perl". If you have internet access, point your browser to http://www.perl.org/ , the Perl homepage.

/ 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 can only be copied under either the Artistic License or the GNU General Public License, which can be found in the original Perl 5 suite.

Full documentation for Perl, including lists of frequently asked questions, should be found on this system using "man perl" or "perldoc perl". If you have internet access, point your browser to http://www.perl.org/ , the Perl homepage.

0
source

Source: https://habr.com/ru/post/1305121/


All Articles