perl 反引号中使用的 tail 命令

Tail command used in perl backticks

我正在尝试 运行 使用常用的反引号从 perl 脚本中执行 tail 命令。

我的perl脚本部分如下:

$nexusTime += nexusUploadTime(`tail $log -n 5`);

所以我试图获取此文件的最后 5 行,但当 perl 脚本完成时出现以下错误:

sh: line 1: -n: command not found

尽管当我 运行 在命令行上执行命令时,它确实成功了,我可以看到那个特定的 5 行。

不确定这里发生了什么。为什么它在命令行下工作,但通过 perl 它无法识别 -n 选项。

有人有什么建议吗?

这个问题的答案是将选项-n 5放在目标文件之前

$log 有一个无关的尾随换行符,所以你正在执行

tail file.log
 -n 5            # Tries to execute a program named "-n"

修复:

chomp($log);

请注意,如果日志 $log 包含 shell 元字符(例如空格),您将 运行 遇到问题。修复:

use String::ShellQuote qw( shell_quote );

my $tail_cmd = shell_quote('tail', '-n', '5', '--', $log);
$nexusTime += nexusUploadTime(`$tail_cmd`);

你的错误,但我建议尽可能避免使用外部命令。它们不可移植,除其他外,调试它们可能很痛苦。您可以像这样用纯 Perl 代码模拟 tail

use strict;
use warnings;

use File::ReadBackwards;

sub tail {
    my ($file, $num_lines) = @_;

    my $bw = File::ReadBackwards->new($file) or die "Can't read '$file': $!";

    my ($lines, $count);
    while (defined(my $line = $bw->readline) && $num_lines > $count++) {
        $lines .= $line;
    }

    $bw->close;

    return $lines;
}

print tail('/usr/share/dict/words', 5);

输出

ZZZ
zZt
Zz
ZZ
zyzzyvas

请注意,如果您传递包含换行符的文件名,这将失败并显示

Can't read 'foo
': No such file or directory at tail.pl line 10.

而不是更神秘的

sh: line 1: -n: command not found

您从 运行 反引号中的 tail 实用程序获得。