Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: If I had a Free Two Months...

by kelan (Deacon)
on Jun 22, 2005 at 15:22 UTC ( #469052=note: print w/replies, xml ) Need Help??

in reply to If I had a Free Two Months...

Is your sweep utility something like this? (Note: untested)

#!/usr/bin/perl use strict; use warnings; use Cwd; my $command = shift; apply( @ARGV ); # takes a directory to start in, and a command # to apply recursively sub apply { my $curdir = cwd; my ( $dir, $command ) = @_; return unless chdir $dir; system $command; if ( not opendir DIR, '.' ) { chdir $curdir; return; } my @children = grep { -d && !/^\.\.?$/ } readdir DIR; closedir DIR; for my $child ( @children ) { apply( $command, $child ); } chdir $curdir; }
Then you execute it like: ./sweep 'dir' 'command -with args'

Replies are listed 'Best First'.
Re^2: If I had a Free Two Months...
by diotalevi (Canon) on Jun 22, 2005 at 16:04 UTC

    Why didn't you use File::Find? That solves the infinite loop problem which you've ignored. It works the same as yours except its less buggy and uses system() with distinct arguments to avoid accidental shell processing.

    #!perl -w use strict; use File::Find; my $start_dir = shift; find( sub { system @ARGV if -d }, $start_dir );

      I knew someone would ask:) I've never used File::Find, which is why I didn't. I don't have much need for recursively processing directories. My post was mostly about showing that it wouldn't take two months to write something similar, rather than present a full solution.

      I used system that way on purpose, so that it would go through shell processing. The OP specifically mentioned wanting to send shell commands through, and those probably would be more useful if the shell got to process them.

        Oh oh. Here's the same non-buggy version that does invoke the shell. To fix the bug in your code, you'll need to follow symlinks and whenever considering entering a directory, decide whether you've been there before. Symlinks will cause your existing code to enter infinite loops and to run the command on some things more than once.

        #!perl -w use strict; use File::Find; my ( $start_dir, $command ) = @ARGV; find( sub { system $command if -d }, $start_dir );

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://469052]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2022-08-18 19:22 GMT
Find Nodes?
    Voting Booth?

    No recent polls found