Useful script perhaps

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Perl script below runs a particular command on multiple files.

I use this script when I've written a bunch of PHP pages that I need to
upload to a server with no PHP support. I create the PHP pages using
".html" extensions and then run: 'php {} >../{}' *.html

Here is the script:

==================== ====================

use Getopt::Long qw(GetOptions);
my $VERSION = '1.1';
my $opts;

    "usage|help|h"     => $opts,
    "version"     => $opts,
    "verbose|v+"    => $opts

if ($opts==1)
    print "$VERSION\n";

&errormessage if ($opts==1);

$cmd = shift @ARGV || &errormessage;
$nfiles = 0;

while ($_ = shift @ARGV)
    $c = $cmd;
    $c =~ s/\/$_/g;      
    print '$ ' . $c . "\n"
        if ($opts==1);

if ($nfiles==0)
    die "Must specify some files!\n"
    print "$nfiles files processed.\n"
        if ($opts==1);

sub errormessage
    print "\n";
    print " - folds a command onto multiple files\n";
    print "Version $VERSION (c) 2005 Toby Inkster\n";
    print "License: \n\n";
    print "USAGE: [options] command file [file...]\n\n";
    print "    \"command\" is a command to run on each file. Use {} to refer to the\n";
    print "    filename (much like the \"find\" command). You may find it benificial\n";
    print "    to use \'single quotes\' around the command.\n\n";
    print "    Many UNIX shells provide similar built-in functionality, but\n";
    print "    has an easier syntax.\n\n";
    print "OPTIONS:\n";
    print "    --verbose -v        Be verbose.\n";
    print "    --usage --help -h   Display help message.\n";
    print "    --version           Display version.\n\n";
    print "EXAMPLES:\n";
    print " \'php {} >../{}.html\' *.php\n";
    print " \'mv {} {}l\' *.htm\n";
    print "\n";

Toby A Inkster BSc (Hons) ARCS
Contact Me  ~

Site Timeline