Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- ############################################
- # eachdir.pl
- # [email protected] Dorfen
- # do it for every dir beneath
- ############################################
- print "eachdir.pl - Thomas Hofmann 2018 - do it for every dir beneath\n";
- # get actually home dir
- my $startdir = `cd`;
- chomp($startdir);
- print "startdir [$startdir]\n";
- # check if there was any par
- my $startdirbak = $startdir;
- if( $#ARGV >= 0 ) {
- my $dirfirst = $ARGV[0];
- if( -d $dirfirst ) {
- $startdir = shift( @ARGV );
- }
- else {
- # abort( "Parameter is no directory [$ARGV[0]]" );
- }
- } else {
- abort( "No Parameter given." );
- }
- # command and pars
- my @comm = @ARGV;
- # reverse process all subdirs
- #print ">>> before dodir; startdir [$startdir] - comm [".join('; ', @comm)."]\n";
- dodir( $startdir, @comm );
- print "*** END ***\n";
- ################################
- sub dodir {
- my ( $startdir, @comm ) = @_;
- print "\t in [$startdir] - call [".join(' ',@comm)."]\n";
- # read dir
- opendir( DIRREAD, $startdir );
- my @dirents = readdir( DIRREAD );
- closedir( DIRREAD );
- # process dir
- my $comm = shift( @comm );
- my @pars = @comm;
- chdir( $startdir );
- my $sysout = system( ($comm, @pars) );
- # check subdirs
- foreach my $actdir( @dirents ) {
- if ( -d $actdir ) {
- dodir( "$startdir/$actdir", ( $comm, @pars ) ) if ( $actdir !~ m/^(\.|\.\.)$/ );
- }
- }
- }
- sub abort {
- my $mes = shift;
- if( $mes ) {
- print "Error: $mes";
- if( $mes !~ m/\n$/ ) { print "\n"; }
- }
- usage();
- exit 255;
- }
- sub usage {
- print "Usage: [perl] eachdir.pl [startdir] command [par1 [par2 ..]]\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment