【问题标题】:How do we use the piped command in Perl? [closed]我们如何在 Perl 中使用管道命令? [关闭]
【发布时间】:2021-07-01 12:48:59
【问题描述】:

这是命令-

$ find /var/opt/ -type f -mtime -1 -print0 | du -s |cut -f1    
498172

当我在 Linux 中从命令行运行时,它会给出输出 - 大小。 我想从 Perl 运行相同的命令,并且需要在变量中捕获输出。

我试过了:

my $cmd = "find /var/opt/ -type f -mtime -1 -print0 | du -s |cut -f1";
my @output = `$cmd`;

我收到一个完全不同的输出 - '\20' 而不是 498172

有人可以帮我解决我所缺少的吗?

【问题讨论】:

    标签: linux perl pipe


    【解决方案1】:

    也可以在Perl脚本中计算大小,无需调用外部命令du

    use feature qw(say);
    use strict;
    use warnings;
    use File::Find;
    
    my $size = 0;
    my $dir = '/var/opt';
    find(sub {-f $_ &&  -M _ < 1 && do {$size += -s _ }}, $dir);
    say int($size/1024), " KiB";
    

    请注意,这报告的是表观大小,而不是磁盘使用情况。请参阅How to get the actual directory size (out of du)? 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      • 2014-06-19
      • 2020-12-14
      相关资源
      最近更新 更多