bash 从bash脚本运行时,Perl“使用:找不到命令”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30061285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:56:36  来源:igfitidea点击:

Perl "use: command not found" when run from bash script

linuxbashperlsyntax-errorcommand-line-interface

提问by fedest

I have a Perl script Info.perlwhich starts like this

我有一个Info.perl像这样开始的 Perl 脚本

#!/usr/bin/perl
#!/usr/bin/bash

use List::Util qw( min max );
use List::MoreUtils qw{ all any };
use Cwd;
use Data::Dumper qw(Dumper);
use Env qw(INFODIR CONFDIR PROCDIR);

And when I run it from the terminal it gives no problem and works as expected.

当我从终端运行它时,它没有问题并且按预期工作。

But then I have a bash script run.shthat only has in it

但是后来我有一个 bash 脚本run.sh,它只包含在其中

./Info.pl

And when I run this script I get the following error

当我运行这个脚本时,我收到以下错误

./Info.pl: line 4: syntax error near unexpected token `('
./Info.pl: line 4: `use List::Util qw( min max );'

If I omit line 4 I get the following message:

如果我省略第 4 行,我会收到以下消息:

./Info.pl: line 5: use: command not found
./Info.pl: line 6: use: command not found
./Info.pl: line 7: syntax error near unexpected token `('
./Info.pl: line 7: `use Data::Dumper qw(Dumper);'

Is there any special way I need to run the Perl script inside the bash script? Or any special way to use the usecommand?

有什么特殊的方法需要在 bash 脚本中运行 Perl 脚本吗?或者使用use命令的任何特殊方式?

回答by Dallaylaen

As has been told in the comments, your script for some reason is getting executed via shell. You can study why it is so and fix it.

正如评论中所说,由于某种原因,您的脚本正在通过 shell 执行。你可以研究为什么会这样并修复它。

Or you can work around using the following trick:

或者您可以使用以下技巧解决:

#!/usr/bin/perl

eval 'exec /usr/bin/perl "##代码##" "$@"'
    if 0; # not running under some shell

print "Args: @ARGV\n";

If this script is evaluated via perl, if 0;line prevents the eval from execution.

如果通过 perl 评估此脚本,则if 0;行会阻止执行 eval。

However, shell doesn't have postfix conditions, so it just executes the first line, which runs the correct interpreter.

但是,shell 没有后缀条件,因此它只执行第一行,该行运行正确的解释器。

In fact, a number, no, a NUMBERof perl scripts start that way. Google this "not running under some shell" for examples (and an explanationon SO).

事实上,有很多,不,有NUMBER个 perl 脚本就是这样开始的。谷歌这个“不在某个shell下运行”的例子(以及对SO的解释)。

回答by Janarth K

Removing '#!/usr/bin/bash' would be a good idea as it is redundant. Calling '/usr/bin/perl Info.pl' in your script may fix the issue.

删除 '#!/usr/bin/bash' 是一个好主意,因为它是多余的。在脚本中调用“/usr/bin/perl Info.pl”可能会解决该问题。