1826372 Members
3766 Online
109692 Solutions
New Discussion

Re: perl question

 
Kris_5
Occasional Advisor

perl question

Dear Perl gurus,

I have typical problem with perl scripting. The requirement is I need to copy files and sub-dirs from one unix staging area(1) to another unix staging area(2) provided staging area(2) do not have the same file and sub-dir. The sub-dir in staging area(1) might have sub-dir's within and again you can copy them when staging area(2) do not have them. To do this check I wrote a small subroutine in perl which checks for files and sub-dir's at top level. Then I am using the same subroutine within to check for sub-dirs within sub-dir in staging area(1). This logic is not working at all. If any of guys provide me some insight on how to use the variables perl subroutine or some logic. Thanks in adv.

Here is the subrouting I am using.
sub xfer_subdir_and_files {
my ($input_source_dir, $input_target_dir) = @_ ;
my (@fl_list, $ind_dir, $ind_fl);

if ( -d $input_source_dir ) {
# Find list of files
@fl_list = `cd $input_source_dir; ll|grep -v \"^d\"|grep -v \"total\"|awk \'{pri
nt \$9}\'`;
foreach $ind_fl (@fl_list) {
chop($ind_fl);
if ( -f "$input_target_dir/$ind_fl" ) {
`echo "Copy failed as the following file already exists in target" >> $xfer_fail_log`;
`echo "$input_source_dir/$ind_fl" >> $xfer_fail_log`;
}
else {
`cp -p $input_source_dir/$ind_fl $input_target_dir/` ;
`echo "Copy successed for following file" >> $xfer_success_log`;
`echo "$input_source_dir/$ind_fl" >> $xfer_success_log`;
} #end else-if ( -f $input_target_dir/
} #end foreach
# Find list of directories
@dir_list = `cd $input_source_dir; ll|grep \"^d\"|awk \'{print \$9}\'`;

# Find list of directories
@dir_list = `cd $input_source_dir; ll|grep \"^d\"|awk \'{print \$9}\'`;
foreach $ind_dir (@dir_list) {
chop($ind_dir) ;
if ( -d "$input_target_dir/$ind_dir" ) {
# call xfer_subdir_and_files sub-routine again.
&xfer_subdir_and_files("$input_source_dir/$ind_dir","$input_target_dir/$ind_dir") ;
`echo "Copy failed as the following dir already exists in target" >> $xfer_fail_log`;
`echo "$input_source_dir/$ind_dir" >> $xfer_fail_log`;
} #end if ( -d
else {
`cp -Rp $input_source_dir/$ind_dir $input_target_dir`;
`echo "Copy successed for following dir" >> $xfer_success_log`;
`echo "$input_source_dir/$ind_dir" >> $xfer_success_log`;
} #end else-if ( -d "$input
}#end foreach
} #end if ( -f
else {
die "\n ERROR: $input_source_dir is not valid source dir \n";
} #end else-if ( -f $input_source_dir )
} #end sub
3 REPLIES 3
Jordan Bean
Honored Contributor

Re: perl question


Is PERL required? As much as love it, I still go with tried and true:

find /top/of/area1 -xdev -depth | cpio -pdmx /top/of/area2

This will only copy all new stuff from area1 to area2... I think. Check the man pages.

I'd work with your code if I had time, but I'm in the middle of storage migration.

H.Merijn Brand (procura
Honored Contributor

Re: perl question

And this is cargo-cult programming.

# perl -MFile::Find -e'find (sub { .... }, )'

# man File::Find

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Gregory Fruth
Esteemed Contributor

Re: perl question

Assuming you still want to hand code this (vs. using a
module as previously suggested), there's a lot of
Perl-ification to be done here.

To get the list of files in a subdirectory, use
opendir/readdir/closedir:

opendir(DIR, $dir) or die;
@files = readdir(DIR);
closedir(DIR);

foreach $file (@files) {
next if ($file eq '.' or $file eq '..');
print "working on file $dir/$file\n" if (-f "$dir/$file");
...
}

Since Perl5 "chop" is pretty much obsolete. Use "chomp"
instead. The "&" on subroutine names is also unnecessary
in Perl5.

Finally, please consider using Perl's "print" instead of spawning
a shell just to do an "echo":

open(FILE, ">$xfer_fail_log") or die;
print(FILE "...\n");
close(FILE);