Solved

Perl tar error

Posted on 2016-11-08
8
65 Views
Last Modified: 2016-11-12
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

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;

Open in new window


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 . . .
0
Comment
Question by:shragi
  • 4
  • 2
  • 2
8 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 41878979
The problem is this line:
tar cvzf $ZipLocation/$zipName $compareDirectory/$name;

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.

my $cmd_putput = `tar cvzf $ZipLocation/$zipName $compareDirectory/$name`;

Open in new window

my $cmd_putput = qx("tar cvzf $ZipLocation/$zipName $compareDirectory/$name");

Open in new window

system("tar cvzf $ZipLocation/$zipName $compareDirectory/$name");

Open in new window

0
 
LVL 28

Expert Comment

by:FishMonger
ID: 41878985
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);

Open in new window

0
 
LVL 28

Expert Comment

by:FishMonger
ID: 41879058
BTW, I forgot to mention that you could use Archive::Tar, which is a core module, instead of shelling out to the system.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 28

Accepted Solution

by:
FishMonger earned 250 total points
ID: 41880917
Here's a cleaned up version of your script.
#!/usr/bin/perl

use 5.010;
use strict;
use warnings;
use POSIX qw(strftime);

my $compare_directory = "/TestData/FolderB";
my $zip_location      = "/TestData/FolderC/";
my $name              = "testfile1.txt";
my $current_date_time = strftime("%Y%m%d%H%M%S", localtime);
my $zip_name          = "${name}_$current_date_time.tar.gz";

say $zip_name;

system("tar -cvzf $zip_location/$zip_name $compare_directory/$name");

Open in new window

0
 

Author Comment

by:shragi
ID: 41881641
Hi FishMonger - The code did not work and did not create tar file and I got an error saying
Error:
tar: Removing leading '/' from member names

Open in new window


#!/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");

Open in new window

0
 
LVL 12

Expert Comment

by:tel2
ID: 41881651
> "...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/FolderC ?
0
 

Author Comment

by:shragi
ID: 41881656
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,
0
 
LVL 12

Assisted Solution

by:tel2
tel2 earned 250 total points
ID: 41881865
Yes.
One way is to remove the leading '/' from this:
    my $compare_directory = "/home/user1/TestData/FolderB";
so it's a relative path, like this:
    my $compare_directory = "home/user1/TestData/FolderB";

And then change this:
    system("tar -cvzf $zip_location/$zip_name $compare_directory/$fname");
so it automatically changes directory to '/' before creating the archive, like this:
    system("tar -C / -cvzf $zip_location/$zip_name $compare_directory/$fname");

See: man tar:
       -C, --directory=DIR
              change to directory DIR

Another way is to prevent tar from stripping the leading '/'.
Again, see: man tar:
       -P, --absolute-names
              don't strip leading `/'s from file names
But I wouldn't recommend this, because it could make your backups harder to restore to where you want them, if you backed them up with a leading '/' in the path.

Another way would be to redirect STDERR (warnings/errors) to somewhere else, but I wouldn't recommend that either, because then you wouldn't see any warnings/errors (e.g. disk space errors).
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PERL get the value for query 4 151
How to strip .csv from file name 9 81
Add additional column to .csv using Perl. 8 141
Perl script to delete older files 6 86
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question