toohoo

eachdir

Dec 5th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.62 KB | None | 0 0
  1. #!/usr/bin/perl
  2. ############################################
  3. # eachdir.pl
  4. # do it for every dir beneath
  5. ############################################
  6.  
  7. print "eachdir.pl - Thomas Hofmann 2018 - do it for every dir beneath\n";
  8.  
  9. # get actually home dir
  10. my $startdir = `cd`;
  11. chomp($startdir);
  12. print "startdir [$startdir]\n";
  13.  
  14. # check if there was any par
  15. my $startdirbak = $startdir;
  16. if( $#ARGV >= 0 ) {
  17.     my $dirfirst = $ARGV[0];
  18.     if( -d $dirfirst ) {
  19.         $startdir = shift( @ARGV );  
  20.     }
  21.     else {
  22. #       abort( "Parameter is no directory [$ARGV[0]]" );
  23.     }
  24. } else {
  25.     abort( "No Parameter given." );
  26. }
  27.  
  28. # command and pars
  29. my @comm = @ARGV;
  30.  
  31. # reverse process all subdirs
  32. #print ">>> before dodir; startdir [$startdir] - comm [".join('; ', @comm)."]\n";
  33. dodir( $startdir, @comm );
  34.  
  35. print "*** END ***\n";
  36.  
  37.  
  38. ################################
  39. sub dodir {
  40.     my ( $startdir, @comm ) = @_;
  41.     print "\t in  [$startdir] - call [".join(' ',@comm)."]\n";
  42.  
  43.  
  44.     # read dir
  45.     opendir( DIRREAD, $startdir );
  46.     my @dirents = readdir( DIRREAD );
  47.     closedir( DIRREAD );
  48.    
  49.     # process dir
  50.     my $comm = shift( @comm );
  51.     my @pars = @comm;
  52.     chdir( $startdir );
  53.     my $sysout = system( ($comm, @pars) );
  54.    
  55.     # check subdirs
  56.     foreach my $actdir( @dirents ) {
  57.         if ( -d $actdir ) {
  58.             dodir( "$startdir/$actdir", ( $comm, @pars ) ) if ( $actdir !~ m/^(\.|\.\.)$/ );
  59.         }
  60.     }
  61. }
  62.  
  63. sub abort {
  64.     my $mes = shift;
  65.     if( $mes ) {
  66.         print "Error: $mes";
  67.         if( $mes !~ m/\n$/ ) { print "\n"; }
  68.     }
  69.     usage();
  70.     exit 255;
  71. }
  72.  
  73. sub usage {
  74.     print "Usage: [perl] eachdir.pl [startdir] command [par1 [par2 ..]]\n";
  75. }
Advertisement
Add Comment
Please, Sign In to add comment