在Perl中运行system()命令时显示进度

我有一个Perl脚本执行一些任务,其中一个调用系统命令“tar -cvf file.tar …..”.

这通常需要一些时间,所以我希望命令行回显一个进度指示器,比如在系统调用正在进行时#echo回到屏幕.

我一直在做一些挖掘,偶然发现叉子.这是最好的方式吗?是否可以分叉系统命令,然后创建一个while循环来检查fork返回的$pid的状态?

我也看过对waitpid的引用….我猜我也需要使用它.

fork system("tar ... ")
while ( forked process is still active) {
    print #
    sleep 1
}

我吠叫错了树吗?

非常感谢
约翰

解决方法

Perl有一个很好的结构,称为“管道打开”.您可以通过在shell提示符下键入perldoc -f open来了解更多相关信息.
# Note the use of a list for passing the command. This avoids
# having to worry about shell quoting and related errors.
open(my $tar,'-|','tar','zxvf','test.tar.gz','-C','wherever') or die ...;

这是一个显示示例的片段:

open(my $tar,...) or die "Could not run tar ... - $!";
  while (<$tar>) {
       print ".";
  }
  print "\n";
  close($tar);

替换打印“.”每隔10到100行打印一个哈希标记,以获得一个漂亮的标尺.

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...