- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- perl question
Operating System - HP-UX
1745936
Members
4061
Online
108723
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-15-2003 03:11 PM
тАО08-15-2003 03:11 PM
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-15-2003 07:38 PM
тАО08-15-2003 07:38 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-16-2003 12:51 AM
тАО08-16-2003 12:51 AM
Re: perl question
And this is cargo-cult programming.
# perl -MFile::Find -e'find (sub { .... },)'
# man File::Find
Enjoy, have FUN! H.Merijn
# perl -MFile::Find -e'find (sub { .... },
# man File::Find
Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-18-2003 10:00 AM
тАО08-18-2003 10:00 AM
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);
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);
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP