需要输出 system() 命令的操作并有进度指示器

发布于 2024-11-16 12:04:52 字数 1038 浏览 13 评论 0原文

我有一个很好的进度指示器 需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俏︾媚 2024-11-23 12:04:52

尝试制作它

 print $LOG $_;

,或者只是

 print $LOG;

(因为如果未指定任何内容, $_ 是默认值)

解释一下: $cmd 是您正在读取的文件; $_ 是从中读取的行。您可以通过编写更清楚:

 while ($line = <$cmd>) {
    print $LOG $line;
    ...
 }

Try making it

 print $LOG $_;

or maybe just

 print $LOG;

(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:

 while ($line = <$cmd>) {
    print $LOG $line;
    ...
 }
清风无影 2024-11-23 12:04:52

$cmd 是命令输出的 I/O 句柄,这是一种内部数据类型,打印起来没有多大意义。您想使用 <$cmd>readline($cmd) 来获取它的输出,您已经在 while 中执行了此操作环形:

while (<$cmd>)      # same as:    while (defined($_ = readline($cmd)))
{
  # last line from command is now stored in $_
  print $LOG $_;

  $percentage = ($cntr/$lines) * 100;
  $percentage = 100 if $percentage > 100;

  printf("Progress: %3d%%\r", $percentage);
  $cntr++;


}

$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> or readline($cmd) to get the output of it, which you already do in your while loop:

while (<$cmd>)      # same as:    while (defined($_ = readline($cmd)))
{
  # last line from command is now stored in $_
  print $LOG $_;

  $percentage = ($cntr/$lines) * 100;
  $percentage = 100 if $percentage > 100;

  printf("Progress: %3d%%\r", $percentage);
  $cntr++;


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文