Answer the question
In order to leave comments, you need to log in
Need help with perl. How to do it right?
I welcome everyone!
Condition:
there are some files in /tmp/src_dir directory:
file1
file2
file3
...
daily these files are copied to /tmp/dst_dir directory and the date of file copying in YYYY-MM-DD format is added to the file name:
...
file1- 2014-06-10
file2-2014-06-10
file3-2014-06-10
...
Task:
write a Perl script that will check if a file has been copied to /tmp/dst_dir during the past day at startup.
For example, today is 2014-06-23, so for every file that exists in /tmp/src_dir there must be a file that is in /tmp/dst_dir/:
file1-2014-06-22
file2-2014-06-22
file3-2014-06-22
If some file\files were not copied, then the script should output the name of the missing file\files.
An important caveat:
the opendir() function cannot be used. The composition of both directories can only be obtained using the ls command.
That is, as I understand it, you need to assign something like this to the array: my @src_dir=`ls /tmp/src_dir`
I don’t even know from which side to approach this task. I will be grateful for any help!
Answer the question
In order to leave comments, you need to log in
For each file approximately such check.
use POSIX qw/strftime/;
#Вчерашняя дата в нужном формате
my $date = strftime('%Y-%m-%d',localtime(time-86400));
my $filepath = "/tmp/dst_dir/file1-$date";
if ! (open $fh, '<', $filepath) {
#Копии файла нет
# как нравится
};
croak $EVAL_ERROR if !close $fh;
Slipeer, thanks for the proposed option, but I have a nuance in the condition: the list of files in directories can only be obtained by calling an external ls.
my @src_dir=`ls /tmp/src_dir`
my @dst_dir=`ls /tmp/dst_dir`
my @src_dir
the same line in my @dst_dir
, but with my $date
at the end of the file name. It is possible like this:
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw/strftime/;
my $src_dir = $ARGV[0];
my $dst_dir = $ARGV[1];
my $yesterday = strftime("%Y-%m-%d", localtime(time-86400));
my @src_files = split /\s+/, `ls $src_dir`;
for my $sf (@src_files){
my $df = "$dst_dir/$sf-$yesterday";
if ( ! -f $df ){
print "$df\n";
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question