LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-07-2008, 12:09 PM   #1
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Rep: Reputation: 33
perl Script trouble shoot (copy files issue)


Hi,

I wrote/copy this script to read file from one directory and copy them to an other.
But in only do partial job.


I ran It as root. it does copy few file then breaks.


Code:


use Cwd;
use File::Copy;

sub mnt{
system("mount server:/home/update-server /mnt/tmp/ ");
if ( $? == -1 )
{
  print "command failed: $!\n";
}
else
{
}
}



sub copyFiles {
    my ($workdir) = shift;

    my($startdir) = &cwd;               #  start Dir.
    chdir($workdir) or die "Unable to enter dir $workdir:$!\n";

    opendir(DIR, ".") or die "Unable to open $workdir:$!\n";
    my @names = readdir(DIR);
    closedir(DIR);

    foreach my $name (@names){
        next if ($name eq ".");
        next if ($name eq "..");

        if (-d $name){                     
            &copyFiles($name);
#             print $name;        
    next;
        }

if (-f $name) {


$workdir =~ s/\/mnt\/tmp//i;
$workdir =~ s/\/etc-update//i;

}
$final= "/tmp/etc-test/$workdir/$name";
#print"/etc/$workdir/$name\n";
print "$final\n";
copy($name, $final )or die "unable to copy file $Workdir/$name\n";
}
#print "startdirectory is $startdir going into it now \n";
    chdir($startdir) or die "Unable to change to dir $startdir:$!\n";
# print "=====$startdir======\n";
}
&mnt;
&copyFiles("/mnt/tmp/etc-update");

Any Ideas?

Thanks
 
Old 10-08-2008, 04:52 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
You want to give a hint about what happens when it "breaks"? Or should we guess! :-)
 
Old 10-08-2008, 09:22 AM   #3
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
I guess I figured out what the issue is.

$final = /start/work/file-------> thats what my final destination look like.

But in some cases it need to be this:
$final= /start/wrok/another-dir/file -------> since directory need to be copied have directoris with directories.

start|
|---------Dir1/file1
|---------Dir2/fie2
|---------Dir3/Dir3a/file3a
|----------|_file3

now when I do copy (file-to-be copied, final)


It only go one step deep.

it does
copy(file-to-be-copy, /start/work/file)
So another-dir get neglected in the path hence it don't copy files /one-dir/two-dir/<here>

Last edited by knockout_artist; 10-08-2008 at 09:27 AM.
 
Old 10-08-2008, 12:12 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Is this case solved, or you still need help?
 
Old 10-08-2008, 01:45 PM   #5
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
I did write a code which worked.
Still I am no artist at writing codes. So You can help If you like.
 
Old 10-08-2008, 02:01 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
The code has some basic logic flaws, some typo-bugs, and in general could be more generalized.

You call:

&mnt

but if the mount fails, you just print a message and continue as if the mount succeeded. This is not good (what's the point of the error check then)?

$Workdir is referenced, but you mean $workdir. Enable

use strict;
use warnings;

to help you find such problems in the future.

You use hard coded values such as mnt\/tmp in your substitution, and also in your mount command. Place such values in variables at the top of your code and use them throughout. Don't hardcode values throughout your code; parametrize and use variables for ease in changes.

Comment your code!

You don't need the &function form. Use parens:

mnt();
copyFiles(...);


Use single quotes instead of double quotes, skipping interpolation when it is not necessary:

".." -> '..'

Finally, you do realize you can do all this with GNU tar, right?
 
Old 10-08-2008, 02:35 PM   #7
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
Thanks a lot for the tips.
I will keep it in mind.

tar ?? really how ?
 
Old 10-08-2008, 02:46 PM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
tar copies a set of files from one location to another. It has various options for including/excluding or pattern replacing source and targets.

Perhaps you can explain exactly which source(s) / target(s) you want, and any exceptions.

The mount of the remote file system can still be in your wrapper script that calls tar.
 
Old 10-08-2008, 03:25 PM   #9
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
Since we are at it, Why that don't work ?

$ntwrk = "service network restart";
$ssh = "service sshd restart";
$sndml ="service sendmail restart";

@services = qw/$ntwrk, $ssh $sndml/;

foreach $services(@services){
#print "$ntwrk\n";
print "$services";

#system($services);

}
~
 
Old 10-08-2008, 09:04 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Here's a cleaned-up version:


Code:
use strict;
use warnings;

my $ntwrk = "service network restart";
my $ssh = "service sshd restart";
my $sndml ="service sendmail restart";
my (@service_list, $service);

@service_list = ($ntwrk, $ssh, $sndml);

foreach $service (@service_list)
{
    print "$service \n";
}
Please use code tags when posting. Also, always use

use strict;
use warnings;

at the top of your code

Read & digest:
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials


PS you can use the -w switch instead of 'use warnings':
#!/usr/bin/perl -w
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
issue with execution of PERL script rotkmaninov Linux - Software 6 07-10-2008 03:47 PM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-23-2008 12:25 AM
How would you trouble-shoot this no Web? dgermann Linux - Networking 4 07-18-2006 09:17 PM
PERL: cgi script copy problem from server to another <db> Programming 2 04-16-2006 10:14 PM
Perl Script issue.... oicdn Linux - Software 2 03-24-2004 11:47 AM


All times are GMT -5. The time now is 04:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration