shragi
asked on
Perl tar error
Hi - right now i know to use the below modules
use Archive::Zip;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS);
to zip the file, but this requires installing this module in the server (cpan Archive::Zip) and which is not allowed in the server as of now.
So I want to use tar cvzf in my perl script and I tried the below code and got an error
Error:
Scalar found where operator expected at timetesting.pl line 22, near "$zipName $
compareDirectory"
(Missing operator before $compareDirectory?)
syntax error at timetesting.pl line 22, near "$zipName $compareDirectory"
Execution of timetesting.pl aborted due to compilation errors.
Press any key to continue . . .
use Archive::Zip;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS);
to zip the file, but this requires installing this module in the server (cpan Archive::Zip) and which is not allowed in the server as of now.
So I want to use tar cvzf in my perl script and I tried the below code and got an error
use 5.010;
use strict;
use warnings;
my $compareDirectory="/TestData/FolderB";
my $ZipLocation="/TestData/FolderC/";
sub getCurrentDateTime(){
my @locTime = localtime();
my $currentDateTime = sprintf("%4d%02d%02d%02d%02d%02d",
1900 + $locTime[5], $locTime[4] + 1, $locTime[3],
$locTime[2], $locTime[1], $locTime[0]);
return $currentDateTime;
}
my $currentDateTime = getCurrentDateTime();
my $name = "testfile1.txt";
my $zipName = $name."_".$currentDateTime.".tar.gz";
print "zipName: $zipName \n";
tar cvzf $ZipLocation/$zipName $compareDirectory/$name;
Error:
Scalar found where operator expected at timetesting.pl line 22, near "$zipName $
compareDirectory"
(Missing operator before $compareDirectory?)
syntax error at timetesting.pl line 22, near "$zipName $compareDirectory"
Execution of timetesting.pl aborted due to compilation errors.
Press any key to continue . . .
Also, get rid of that getCurrentDateTime() sub and instead use the strftime function from the POSIX (core) module.
use POSIX qw(strftime);
my $current_date_time = strftime("%Y%m%d%H%M%S", localtime);
BTW, I forgot to mention that you could use Archive::Tar, which is a core module, instead of shelling out to the system.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi FishMonger - The code did not work and did not create tar file and I got an error saying
Error:
Error:
tar: Removing leading '/' from member names
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use POSIX qw(strftime);
my $compare_directory = "/home/user1/TestData/FolderB";
my $zip_location = "/home/user1/TestData/FolderC";
my $fname = "testfile1.txt";
my $name = "testfile1";
my $current_date_time = strftime("%Y%m%d%H%M%S", localtime);
my $zip_name = "${name}_$current_date_time.tar.gz";
print $zip_name;
system("tar -cvzf $zip_location/$zip_name $compare_directory/$fname");
> "...The code did not work and did not create tar file and I got an error saying
Error:"
Did it actually say "Error"? That should be a warning, and is just telling you that it's making the paths relative so you can restore it to anywhere, not just to the original root position, which is good news.
Are you sure it didn't work?
What files do you see in /home/user1/TestData/Folde rC ?
Error:"
Did it actually say "Error"? That should be a warning, and is just telling you that it's making the paths relative so you can restore it to anywhere, not just to the original root position, which is good news.
Are you sure it didn't work?
What files do you see in /home/user1/TestData/Folde
ASKER
that's true its not an error and it worked, sorry i gave the wrong path.
Is there a way to avoid that warning.
Thanks,
Is there a way to avoid that warning.
Thanks,
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
If you intended to execute that command, you need to wrap it in ` ` backticks, or the qx() operator, or the system() function.Open in new window
Open in new window
Open in new window