[Gllug] Poor scripting?!

Henrik Morsing henrik at morsing.cc
Tue Mar 11 12:49:08 UTC 2008


Hi all, I've come across a script at work that just looks like incredibly poor scripting to me. All they need to do is rename some directories, if the directory matches "*cerprod", change it to "$CRMDB".

To do this they call find twice and each find exec's a Perl script on the directory! First of all, can anyone firgure out why the find commands are run twice?

#These are run twice
echo "renaming cerprod directories once"
find /cerillion -depth -type d -name "*cerprod" -exec /sysadmin/scripts/rename s/cerprod/$CRMDB/g {} \;
echo "renaming cerprod directories twice"
 
find /cerillion -depth -type d -name "*cerprod" -exec /sysadmin/scripts/rename s/cerprod/$CRMDB/g {} \;
 
#These are run twice
echo "renaming medprod directories once"
find /cerillion -depth -type d -name "*medprod" -exec /sysadmin/scripts/rename s/medprod/$MEDDB/g {} \;
echo "renaming medprod directories twice"
find /cerillion -depth -type d -name "*medprod" -exec /sysadmin/scripts/rename s/medprod/$MEDDB/g {} \;


The next thing is the script "rename" it calls. It's a Perl script (!) and basically just regexp and changes the directory name:

#!/usr/local/bin/perl
#
# Usage: rename perlexpr [files]

($regexp = shift @ARGV) || die "Usage:  rename perlexpr [filenames]\n";

if (!@ARGV) {
   @ARGV = <STDIN>;
   chomp(@ARGV);
}


foreach $_ (@ARGV) {
   $old_name = $_;
   eval $regexp;
   die $@ if $@;
   rename($old_name, $_) unless $old_name eq $_;
}

exit(0);


This looks unbelieably poor to me. Call find twice, bad, -exec a Perl script on each directory, even worse, Perl script does nothing sed couldn't have done, really bad.

I'm thinking one find and maybe pipe to sed and xargs? The current script takes 1:30h to run, what would be the most efficient way of doing this?

Thanks a lot for any hints... 

Regards,
Henrik Morsing
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list