简体   繁体   English

从perl脚本调用系统命令,打印输出并将其存储到变量中

[英]Call system command from perl script printing the output AND storing it into a variable

I want to send a command, for example, execute a ping: 我要发送命令,例如执行ping命令:

ping google.es -n 500

I can execute the command with: 我可以执行以下命令:

my $command =  "ping google.es -n 500";
system($command);

And I will get the output printed while it runs. 我将在运行时打印输出。

I can store it into a variable with: 我可以将其存储到一个变量中:

my $command =  "ping google.es -n 500";
my $output = `$command`;

But how can I get the output of the command WHILE it is running and then use the output in a variable? 但是,我怎么能得到命令的输出, 它正在运行,然后使用输出变量?

I´ve see a solution like this, but it does not work. 我已经看到了这样的解决方案,但是它不起作用。 It runs the script and when it has finished it will print the result: 它运行脚本, 完成后将打印结果:

my $variable = '';
open my $command_pipe, "-|", $command or die "Pipe from $command failed: $!";
while (<$command_pipe>) {
    print  $_;
    $variable . = $_;
}

UPDATE : Example to show the problem. 更新 :示例来显示问题。 I have 2 perl scripts and one calls the other: 我有2个perl脚本,一个调用另一个:

test.pl: test.pl:

use strict;
use warnings;

my $command = 'perl test2.pl';

print "Localtime: " . localtime() . "\n";

my $variable = '';
open my $command_pipe, "-|", $command or die "Pipe from $command failed: $!";
while (<$command_pipe>) {
    print "[" .  localtime() . "] " . $_;
    $variable .= $_;
}

print "Localtime: " . localtime() . "\n";
print "Loop finished:\n$variable\n";

Second file test2.pl: 第二个文件test2.pl:

use strict;
use warnings;


print "Waiting 10 sec\n";
my $i = 0;
while($i < 10){
    sleep(1);
    print "$i\n";
    $i++;
}
print "That's it!\n";

And the output is: 输出为:

C:\Users\****\Desktop\****>perl test.pl
Localtime: Wed Oct 17 13:47:06 2018
[Wed Oct 17 13:47:16 2018] Waiting 10 sec
[Wed Oct 17 13:47:16 2018] 0
[Wed Oct 17 13:47:16 2018] 1
[Wed Oct 17 13:47:16 2018] 2
[Wed Oct 17 13:47:16 2018] 3
[Wed Oct 17 13:47:16 2018] 4
[Wed Oct 17 13:47:16 2018] 5
[Wed Oct 17 13:47:16 2018] 6
[Wed Oct 17 13:47:16 2018] 7
[Wed Oct 17 13:47:16 2018] 8
[Wed Oct 17 13:47:16 2018] 9
[Wed Oct 17 13:47:16 2018] That's it!
Localtime: Wed Oct 17 13:47:16 2018
Loop finished:
Waiting 10 sec
0
1
2
3
4
5
6
7
8
9
That's it!

As you can see everything is just printed after the 10 seconds of the second script. 如您所见,所有内容仅在第二个脚本的10秒后打印。

This code works exactly as expected for me (I changed the syntax of your ping call slightly). 这段代码对我来说完全符合预期(我对您的ping调用的语法进行了少许更改)。

#!/usr/bin/perl

use strict;
use warnings;

my $command = 'ping -c 5 google.es';

my $variable;
open my $command_pipe, "-|", $command or die "Pipe from $command failed: $!";
while (<$command_pipe>) {
    print  $_;
    $variable .= $_;
}

print "Loop finished:\n$variable\n";

If it's not working for you, you might be Suffering from Buffering . 如果对您不起作用,那么您可能正在遭受Buffering的困扰 Alternatively, can you give us a complete, self-contained program that demonstrates your problem? 或者,您可以给我们提供一个完整的,自包含的程序来演示您的问题吗?

Update: As I suggested above, you're Suffering from Buffering. 更新:正如我上面建议的那样,您正在遭受缓冲的困扰。 Did you read the article that I linked to? 您是否阅读了我链接的文章? All you need to do is to switch off buffering on STDOUT. 您需要做的就是关闭STDOUT上的缓冲。

$|++;

Update 2: To be clear, you need to put that code in test2.pl - that's the program that's doing the buffered prints, so that's where you need to turn buffering off. 更新2:明确地说,您需要将该代码放入test2.pl -这是正在执行缓冲打印的程序,因此需要关闭缓冲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM