需要输出 system() 命令的操作并有进度指示器
我有一个很好的进度指示器 需要使用 T:R:G mod 的 Perl system() 命令的进度指示器
open(my $cmd, '-|', "$command $flags_args 2>/dev/null")
or print "\nAttention: Command $command $flags_args failed $!"
and return 1;
while (<$cmd>)
{
$percentage = ($cntr/$lines) * 100;
$percentage = 100 if $percentage > 100;
printf("Progress: %3d%%\r", $percentage);
$cntr++;
}
close($cmd);
现在我的想要记录命令输出的 STDOUT 和 STDERR。我对管道输出不太熟悉,所以我尝试
print $LOG $cmd
添加:和
print $LOG Dumper(\$cmd)
在 while 循环结束之前 。它不起作用。第一个输出
GLOB(0x11df7a0)GLOB(0x11df7a0)GLOB(0x11df7a0)
第二个
$VAR1 = \\*{'::$cmd'};
$VAR1 = \\*{'::$cmd'};
$VAR1 = \\*{'::$cmd'};
有谁知道如何从管道 $cmd 获取输出? $command = make 命令的示例
Making all in src
make[1]: Entering directory `/tmp'
Making all in include
make[2]: Entering directory '/tmp/2'
...
I have a nice progress indicator from Need a progress indicator for a Perl system() command using T:R:G mod
open(my $cmd, '-|', "$command $flags_args 2>/dev/null")
or print "\nAttention: Command $command $flags_args failed $!"
and return 1;
while (<$cmd>)
{
$percentage = ($cntr/$lines) * 100;
$percentage = 100 if $percentage > 100;
printf("Progress: %3d%%\r", $percentage);
$cntr++;
}
close($cmd);
Now my I want logging of the STDOUT and STDERR of the commands output. I'm not too familiar with pipe output so I tried adding:
print $LOG $cmd
and
print $LOG Dumper(\$cmd)
before the end of the while loop. It did not work. The first output
GLOB(0x11df7a0)GLOB(0x11df7a0)GLOB(0x11df7a0)
the second
$VAR1 = \\*{'::$cmd'};
$VAR1 = \\*{'::$cmd'};
$VAR1 = \\*{'::$cmd'};
Does anyone know how I can get the output from the piped $cmd?
Example for a $command = make command
Making all in src
make[1]: Entering directory `/tmp'
Making all in include
make[2]: Entering directory '/tmp/2'
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试制作它
,或者只是
(因为如果未指定任何内容,
$_
是默认值)解释一下:
$cmd
是您正在读取的文件;$_
是从中读取的行。您可以通过编写更清楚:Try making it
or maybe just
(since
$_
is the default if nothing is specified)To explain:
$cmd
is the file you're reading from;$_
is the line read from it. You can make it clearer by writing:$cmd
是命令输出的 I/O 句柄,这是一种内部数据类型,打印起来没有多大意义。您想使用<$cmd>
或readline($cmd)
来获取它的输出,您已经在while
中执行了此操作环形:$cmd
is the I/O handle to your command's output, an internal data type that doesn't make much sense to print. You want to use<$cmd>
orreadline($cmd)
to get the output of it, which you already do in yourwhile
loop: