backscratcher

 

fx

Page history last edited by tbarron 11 mos ago

fx

 

Notes

 

  • 2008.1219 - write python version
  • 2008.1219 - write perldoc
  • 2008.1219 - add GPL notice


 


#!/usr/bin/perl

require("getopts.pl");
# -c <command>: 
# -e <substitution>: specify the substitution to apply to the filenames
# -i: generate a range of numbers
# -n: don't actually do it, just show what would happen
# -q: quiet, don't echo commands, just run them
# -x: batch arguments from stdin into command lines like xargs
Getopts("c:e:i:nqx");

$Home = $ENV{"HOME"};

if ($opt_e)
{
   SubstRename(@ARGV);
}
elsif ($opt_i)
{
   IterateCommand($opt_c, @ARGV);
}
elsif ($opt_x)
{
   BatchCommand($opt_c, @ARGV);
}
elsif ($opt_c)
{
   SubstCommand($opt_c, @ARGV);
}
else
{
   Usage();
}

# ===========================================================================
sub BatchCommand
{
   local($runnable, @rest) = @_;

   $cmd = $runnable;

   foreach $line (<STDIN>)
   {
      chomp($line);
      foreach $item (split(" ", $line))
      {
         $cmd =~ s/%/$item %/g;
         $cmd =~ s/~/$Home/g;
         $pending = 1;

         if (240 < length($cmd))
         {
            $cmd =~ s/s*%s*//g;
            psys("$cmd");
            $pending = 0;
            $cmd = $runnable;
         }
      }
   }
   if ($pending)
   {
      $cmd =~ s/s*%s*//g;
      psys("$cmd");
   }
}

# ===========================================================================
sub IterateCommand
{
   local($runnable, @rest) = @_;

   my ($low, $high) = split(/:/, $opt_i);
   for ($idx = $low ; $idx != $high ; $idx++)
   {
      $cmd = $runnable;
      $cmd =~ s/%/$idx/g;
      $cmd =~ s/~/$Home/g;

      psys("$cmd");
   }
}

# ===========================================================================
sub psys
{
   my ($cmd) = @_;

   if ($opt_n)
   {
      print("would do '$cmd'n");
   }
   elsif ($opt_q)
   {
      system($cmd);
   }
   else
   {
      print("$cmdn");
      system($cmd);
   }
}

# ===========================================================================
sub SubstCommand
{
   local($runnable, @rest) = @_;

   foreach $filename (@rest)
   {
      $cmd = $runnable;
      $cmd =~ s/%/$filename/g;
      $cmd =~ s/~/$Home/g;

      psys($cmd);
   }
}

# ===========================================================================
sub SubstRename
{
   foreach $filename (@ARGV)
   {
      # print "$filenamen";
      eval("($newname = $filename) =~ $opt_e");

      print "rename $filename $newnamen";
      if (!$opt_n)
      {
         rename($filename, $newname);
      }
   }
}

# ===========================================================================
sub Usage
{
   print "n";
   print "   fx [-n] -c <command> <files> (% in the cmd becomes filename)n";
   print "           -e s/old/new/ <files> (rename file old to new name)n";
   print "           -i low:high <command> (% ranges from low to high)n";
   print "n";
}

__END__

=head1 NAME

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 AUTHOR

Tom Barron 

Comments (0)

You don't have permission to comment on this page.