Automation in Perl script
I am running a perl script. In my perl script, I check the current date and the folder name(which is also in the date format like for example 11-12-07). This perl script run automatically when It checks the curent date with the folder name. The folder is a tar folder which is loaded from other server.
So, basically I need to run the script if it matched with the folder name and current date.
Problem: Sometimes, I used to get the folder next day and my perl script checks only for the current date. The folder i get has the name which is previous date (not the current date).So, I need to do processing of the folder manually. I need to automate it in my perl script.
Please suggest me some ideas to make it happen.
Thanks!!
Code for Reference:
my $tfilename = 'tarmd5.tar';
my $td = `date '+%y-%m-%d'`; # date in yy-mm-dd format
chomp ($td);
my $td2 = `date '+%Y%m%d'`; # date in yyyymmdd format
chomp ($td2);
#
# get directory from command line
$dir = shift;
leave("'$dir' is not a valid directory") unless (-d $dir);
if ($dir eq '.') {$dir = cwd();}
elsif ($dir !~ /^\//) {$dir = cwd()."/$dir";}
# print out the time
print scalar(localtime()),"\n";
######## This section unpacks transferred data ########
# go to directory for today and find *tar.gz files to copy
my $dday = "$dir/$td";
next unless (-d "$dday");
@gzfiles = glob("$dday/*tar.gz");
foreach $zf(@gzfiles) {
next if (($zf =~ /BMP/) || ($zf =~ /LG/) || ($zf =~ /MAP/) || ($zf =~ /STR/));
print "$zf\n";
($stat开发者_运维技巧us,$message) = systemcall("/bin/cp $zf $fdir");
}
Maybe using DateTime to do the math. I redid the solution as the first was poorly written. Changed DateTime->today
to DateTime->now
because one wants the hms portion when converting back to the desired time zone (from 'floating' or 'UTC').
Also used Perl functions instead of shelling out to the Unix system, (date functions, current working directory - cwd, and copy function).
Update: elsif ($dir != /^\//)
is incorrect. Changed to elsif ($dir !~ /^\//)
.
#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
use DateTime;
use File::Copy;
# set to your desired time zone
my $today = DateTime->now( time_zone => "America/New_York" );
my $td = $today->strftime("%y-%m-%d");
# strongly recommended to do date math in the 'floating'/UTC zone
my $yesterday = $today->set_time_zone('floating')->subtract( days => 1);
my $yd = $yesterday->set_time_zone('America/New_York')->strftime("%y-%m-%d");
my $dir = shift or die "Provide path on command line. $!";
if ($dir eq '.') {
$dir = cwd;
}
elsif ($dir !~ /^\//) {
$dir = cwd() . "/$dir";
}
opendir my $dh, $dir or die $!;
my @dir = sort grep {-d and /$td/ || /$yd/} readdir $dh;
closedir $dh or die $!;
@dir or die "Found no date directories. $!";
my $dday = "$dir/$dir[-1]"; # is today unless today not found, then yesterday
my $fdir = '/some/example/path/';
my @gzfiles = glob("$dday/*tar.gz");
foreach my $zf (@gzfiles) {
next if (($zf =~ /BMP/) || ($zf =~ /LG/) || ($zf =~ /MAP/) || ($zf =~ /STR/));
print "$zf\n";
copy($zf, $fdir) or die "Unable to copy. $!";
}
So you want to get all the directory names that match the current day or any previous days? I presume you move the directories somewhere else when they're done being processed.
A good place to start is the DateTime module. Getting the current date is easy enough:
my $now = DateTime->now();
Then you need to iterate through all directories and pick out the dates you want. Use "perldoc -f" to lookup opendir(), readdir(), and closedir() for getting the directories. To match them, parse out the day/month/year, and create another DateTime object:
my $dir_date = DateTime->new(
day => $dir_day,
month => $dir_month,
year => $dir_year,
);
Once you have all that together, finding if the given directory is a hit is as easy as:
processDir( $dir_name )
if DateTime->compare( $now, $dir_date ) >= 0;
I wonder if it wouldn't be simpler with a bash script. If I understand what you are trying to do, it is
- find recent .tar.gz files, with names not containing "BMP", "LG", etc.
- copy these files to another dir ($fdir, which is undefined in your example)
Maybe you could just ignore the whole folder-name problem, and search for files not older than 24 hours?
dir=/your/bas/dir
fdir=/your/destination
find $dir -iname "*.tar.gz" -mtime -1 -not \( -name "*BMP*" -o -name "*LG*" -o -name "*MAP*" \) -exec cp "{}" "$fdir" \;
精彩评论