为什么在运行Perl脚本时要使用“use: command not found”?

时间:2021-10-12 20:44:56

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

我新Perl。我使用了来自一个论坛的以下代码连接到一个服务器。但是扔错误消息

[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::Telnet ( Timeout=>2, Errmode=>'die');'

6 个解决方案

#1


12  

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.

我的猜测是,您使用的是一种不尊重#的奇怪的unix风格!并尝试通过shell而不是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.

另一个原因是如果tt。pl以空行开始。# !必须在文件的最开始出现。

Try running perl tt.pl and see what happens.

尝试运行perl tt。看看会发生什么。

#2


4  

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.

我有类似的错误消息,并发现原因是perl文件使用了错误的字符编码(不要问我为什么这对perl很重要)。Perl安装正确,路径正确,脚本语法完美(我甚至得到了一行“Hello World!”脚本的“use: command not found”错误)。检查tt。pl是UTF8 no-BOM。

#3


3  

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

正如一条评论所言:“看起来一开始就有CR。”如果你绝望地在这里……然后运行d2u script.pl

d2u --> dos to unix.

d2u——>到unix。

#4


1  

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

我在您的脚本顶部看不到这些行,它们对于所有perl模块和脚本都是必不可少的:

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.

您没有说它在哪个环境中运行——您在linux上使用bash吗?无论您使用的是什么shell,都不理解shebang (#!/usr/bin/perl),并且正在尝试用自己的语言执行脚本,而不是调用perl来运行它。

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

尝试使用/bin/bash,然后您的shebang行就可以工作了。或者,只需显式地调用perl: perl tt.pl。

#5


0  

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

如果/usr/bin/perl文件为空,也会发生这种情况

#6


0  

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.

我遇到了同样的问题,发现#!/usr/bin/perl中没有perl。perl -v或/usr/bin/perl -v都不能工作。

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

我为perl做了一个find / -name,然后添加了一个从/usr/bin到实际位置的符号链接

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

perl -v -bash: perl:未找到/usr/二进制/perl -v -bash: /usr/二进制/perl:没有这样的文件或目录

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

找到/ -name perl /opt/ISS/bin/perl5/perl /opt/ISS/bin/perl

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

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

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

这是为x86_64-linux构建的perl 5、版本14、subversion 1 (v5.14.1)

Copyright 1987-2011, Larry Wall

版权1987 - 2011,拉里墙

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.

Perl只能在art许可证或GNU通用公共许可证的条款下进行复制,这可以在Perl 5源代码工具包中找到。

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.

应该在这个系统上使用“man Perl”或“perldoc Perl”来找到完整的Perl文档,包括FAQ列表。如果您可以访问Internet,请将浏览器指向http://www.perl.org/,即Perl主页。

/usr/bin/perl -v

/usr/bin/perl - v

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

这是为x86_64-linux构建的perl 5、版本14、subversion 1 (v5.14.1)

Copyright 1987-2011, Larry Wall

版权1987 - 2011,拉里墙

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.

Perl只能在art许可证或GNU通用公共许可证的条款下进行复制,这可以在Perl 5源代码工具包中找到。

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.

应该在这个系统上使用“man Perl”或“perldoc Perl”来找到完整的Perl文档,包括FAQ列表。如果您可以访问Internet,请将浏览器指向http://www.perl.org/,即Perl主页。

#1


12  

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.

我的猜测是,您使用的是一种不尊重#的奇怪的unix风格!并尝试通过shell而不是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.

另一个原因是如果tt。pl以空行开始。# !必须在文件的最开始出现。

Try running perl tt.pl and see what happens.

尝试运行perl tt。看看会发生什么。

#2


4  

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.

我有类似的错误消息,并发现原因是perl文件使用了错误的字符编码(不要问我为什么这对perl很重要)。Perl安装正确,路径正确,脚本语法完美(我甚至得到了一行“Hello World!”脚本的“use: command not found”错误)。检查tt。pl是UTF8 no-BOM。

#3


3  

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

正如一条评论所言:“看起来一开始就有CR。”如果你绝望地在这里……然后运行d2u script.pl

d2u --> dos to unix.

d2u——>到unix。

#4


1  

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

我在您的脚本顶部看不到这些行,它们对于所有perl模块和脚本都是必不可少的:

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.

您没有说它在哪个环境中运行——您在linux上使用bash吗?无论您使用的是什么shell,都不理解shebang (#!/usr/bin/perl),并且正在尝试用自己的语言执行脚本,而不是调用perl来运行它。

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

尝试使用/bin/bash,然后您的shebang行就可以工作了。或者,只需显式地调用perl: perl tt.pl。

#5


0  

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

如果/usr/bin/perl文件为空,也会发生这种情况

#6


0  

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.

我遇到了同样的问题,发现#!/usr/bin/perl中没有perl。perl -v或/usr/bin/perl -v都不能工作。

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

我为perl做了一个find / -name,然后添加了一个从/usr/bin到实际位置的符号链接

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

perl -v -bash: perl:未找到/usr/二进制/perl -v -bash: /usr/二进制/perl:没有这样的文件或目录

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

找到/ -name perl /opt/ISS/bin/perl5/perl /opt/ISS/bin/perl

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

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

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

这是为x86_64-linux构建的perl 5、版本14、subversion 1 (v5.14.1)

Copyright 1987-2011, Larry Wall

版权1987 - 2011,拉里墙

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.

Perl只能在art许可证或GNU通用公共许可证的条款下进行复制,这可以在Perl 5源代码工具包中找到。

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.

应该在这个系统上使用“man Perl”或“perldoc Perl”来找到完整的Perl文档,包括FAQ列表。如果您可以访问Internet,请将浏览器指向http://www.perl.org/,即Perl主页。

/usr/bin/perl -v

/usr/bin/perl - v

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

这是为x86_64-linux构建的perl 5、版本14、subversion 1 (v5.14.1)

Copyright 1987-2011, Larry Wall

版权1987 - 2011,拉里墙

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.

Perl只能在art许可证或GNU通用公共许可证的条款下进行复制,这可以在Perl 5源代码工具包中找到。

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.

应该在这个系统上使用“man Perl”或“perldoc Perl”来找到完整的Perl文档,包括FAQ列表。如果您可以访问Internet,请将浏览器指向http://www.perl.org/,即Perl主页。