CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
system

 



perlfarmer
User

Nov 30, 2009, 3:28 PM

Post #1 of 3 (372 views)
system Can't Post

I am trying to write a script the will either delete or rename all of the subdirectories in a directory. If the subdirectory contains a file with a title like defects_AMD then I save the contents of that file and rename the subdirectory. Otherwise I delete the subdirectory. Here is the relevant code fragment:


Code
my @dupsList; 
my @directories = <*>;
foreach my $directory (@directories)
{
my @defectsAMD;
if (-d $directory)
{
my @defectFiles = <$directory/defects_AMD*>;
open(DUPS_IN, "<", $defectFiles[0]) or die "Unable to open file '$defectFile': $!\n";
while (my $line = <DUPS_IN>)
{
push(@defectsAMD, "$line\n");
}
close(DUPS_IN);
}
if (@defectsAMD)
{
push(@dupsList, @defectsAMD);
my $newName = $directory . "_unmerged";
system("mv", $directory, $newName);
}
else
{
system("rm -rf", $directory);
}
}


The system call in the if clause is succeeding and directories are being renamed. However, the system call in the else clause is not succeeding; the error message is:


Code
Can't exec "rm -rf": No such file or directory at ../../processDirectory.pl


The file ../../processDirectory.pl is the script that is running and it appears that it is looking to remove subdirectories in that directory. However, even when I specify the full path name to the subdirectories I want to remove I get the same error message. Also, I don't understand why one system call is finding the correct subdirectories and the other one isn't.

Later in the code I have:


Code
open(DUPS_OUT, ">", $dupsFile) or die "Unable to open file '$dupsFile': $!\n"; 
print "Writing dups to file '$dupsFile'\n";
print(DUPS_OUT @dupsList);
close(DUPS_OUT);


But when I run another script that has to process $dupsFile I get this error:


Code
The cursor is not positioned on a valid current row.


I am wondering if this means that the read position for that file is somehow set to the end of the file and, if so, how to fix it.

Any advice is appreciated.


perlfarmer
User

Nov 30, 2009, 5:01 PM

Post #2 of 3 (369 views)
Re: [perlfarmer] system [In reply to] Can't Post

The error about the cursor can be ignored. It is not a Perl error.


7stud
Enthusiast

Nov 30, 2009, 9:02 PM

Post #3 of 3 (364 views)
Re: [perlfarmer] system [In reply to] Can't Post


Code
system('rm', '-rf', $directory);


If you use the multiple argument form of system(), you have to go all the way.

By the way, system() calls aren't free. Starting a separate process to execute a command has its own overhead. perl has all the necessary functions to accomplish what you are trying to do without resorting to system(), like the glob operator(you're actually using that but with the old syntax <*>), opendir(), readdir(), File::Path::rmtree(), and rename(). It may be that rm -rf is faster, but unless you know that, and you need the extra speed, stick to perl. The perl functions are going to be portable, while the commands you execute in your system() calls may not be.


(This post was edited by 7stud on Nov 30, 2009, 10:20 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives