J
J
johny692014-06-23 13:29:26
Perl
johny69, 2014-06-23 13:29:26

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

3 answer(s)
S
Slipeer, 2014-06-23
@Slipeer

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;

J
johny69, 2014-06-23
@johny69

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`

Those. as I understand it, it is necessary to check whether for each line from my @src_dirthe same line in my @dst_dir, but with my $dateat the end of the file name.
How would it be implemented?

E
Elena Bolshakova, 2014-07-14
@liruoko

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";
    }
}

Call like this:
If there are high requirements for the reliability of the script, it is worthwhile to call the external ls more carefully (check that it has not crashed).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question