tags:

views:

248

answers:

2

When using gtar to add extra files to an existing archive, the loop terminates prematurely or hangs. It also terminates after creating the initial tar.gz file.

However, if I remove the gtar calls from the loop and put print statements in their place instead, the loop executes as expected. Does anyone know why this is? Below is the code contained inside the loop.

if (-e "flex_$yearA"."_"."$monthA.tar.gz")
{ print"accessing Flex tar \n";
 exec "gtar --append --file=flex_$yearA"."_"."$monthA.tar.gz $FILE";
}
else
{ print "creating Flex Tar \n ";
 exec "gtar -cvsf flex_$yearA"."_"."$monthA.tar.gz $FILE"; 
}
+2  A: 
* exec LIST

* exec PROGRAM LIST

The exec function executes a system command and never returns-- use system instead of exec if you want it to return. It fails and returns false only if the command does not exist and it is executed directly instead of via your system's command shell (see below).

So , in conclusion , use system or backticks ` . Exec replaces the execution of your script with another one .

This should work as desired.


if (-e "flex_$yearA"."_"."$monthA.tar.gz")
{ print"accessing Flex tar \n";
 system "gtar --append --file=flex_$yearA"."_"."$monthA.tar.gz $FILE";
}
else
{ print "creating Flex Tar \n ";
 system "gtar -cvsf flex_$yearA"."_"."$monthA.tar.gz $FILE"; 
}


Geo
+5  A: 

You want "system", not "exec". Here's a cleaner version:

my $tarball = "flex_${yearA}_${monthA}.tar.gz";

if ( -e $tarball ) { 
    print"accessing Flex tar \n";

    my $command = "gtar --append --file=$tarball $FILE";
    system($command) == 0
      or die "Could not ($command): $?";
}
else{ 
    print "creating Flex Tar \n ";
    my $command =  "gtar -cvsf $tarball $FILE";
    system($command) == 0
      or die "Could not ($command): $?";
}

However, I'm wondering where all of those variables come from. You could expose a serious security hole here. Read "perldoc -f system" for more information about passing a list to system (safer).

Ovid