在Perl中捕获BZIP2终止

时间:2013-04-15 16:36:17

标签: perl bzip2

这是我到目前为止所做的:

#!/usr/bin/perl

use strict;
use warnings;

use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error);
use File::Find;
use File::Basename;

my $directory = "/usr/www/op1/public/server";
my @list_of_files;

find sub {
        return if -d;
        my $current = basename("$File::Find::name");

        if ($current =~ /\.bz2$/i)
        {
                #print "<$current> is already compressed! Moving along!\n";
                return;
        }

        print "<$current> is being compressed!\n";
        bzip2 $File::Find::name => "$File::Find::name.bz2";

        if (!("$Bzip2Error" eq ""))
        {
                print "\n$Bzip2Error at $File::Find::name\n";
                print "Trying again to compress <$current>\n";
                bzip2 $File::Find::name => "$File::Find::name.bz2" or print "FAILURE 2!!!";
        }
}, "$directory";

基本上,如果它决定终止,则进行bzipping:

<bobsagot> is being compressed!
<arthur.bsp.bz2> is already compressed! Moving along!
<reed.bsp.bz2> is already compressed! Moving along!
<bobby.bsp.bz2> is already compressed! Moving along!
<bigjoe.bsp> is being compressed!
Terminated

它会立即从失败的地方继续拾取并继续前进,但是我没有从$Bzip2Error打印出错误,所以我不知所措。如何捕获bzip2失败的事件?

谢谢!

2 个答案:

答案 0 :(得分:0)

您是否在代码中出现了剪切错误?以“系统(”)开头的行不合适。

bzip2 调用期间脚本是否自行终止?

如果你写这样的代码会发生什么?

if ( ! bzip2 $File::Find::name => "$File::Find::name.bz2")
{
    print "\nBzip Errir is [$Bzip2Error] System Error is [$!] at $File::Find::name\n";
}

答案 1 :(得分:0)

共享主机限制perl执行时间为60秒。失去希望:D