我有一个 Perl 脚本 Info.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);
当我从终端运行它时,它没有出现任何问题并且按预期工作。
但是我有一个 bash 脚本 run.sh
其中仅包含
./Info.pl
当我运行此脚本时,出现以下错误
./Info.pl: line 4: syntax error near unexpected token `('
./Info.pl: line 4: `use List::Util qw( min max );'
如果我省略第 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);'
我需要用什么特殊方法在 bash 脚本中运行 Perl 脚本吗?或者有什么特殊的方式来使用 use
命令?
最佳答案
正如评论中所述,由于某种原因,您的脚本是通过 shell 执行的。您可以研究为什么会这样并解决它。
或者您可以使用以下技巧来解决:
#!/usr/bin/perl
eval 'exec /usr/bin/perl "$0" "$@"'
if 0; # not running under some shell
print "Args: @ARGV\n";
如果通过 perl 计算此脚本,则 if 0;
行会阻止 eval 执行。
但是,shell 没有后缀条件,因此它只执行第一行,该行运行正确的解释器。
事实上,很多,不,NUMBER 个 perl 脚本就是这样开始的。谷歌这个“不在某些外壳下运行”作为示例(以及 explanation )。
关于linux - 从 bash 脚本运行时 Perl "use: command not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30061285/