#!/usr/bin/perl -w =head1 NAME foreach - compose and execute dynamic command lines. =head1 SYNOPSIS foreach [-hnvd] cmdline =head1 DESCRIPTION foreach is a command that takes input as a series of lines, and allows you to build and execute strings that are executed as commands. Given command lines are searched for three tags: $File - which gets replaced with the input line. $file - which gets replaced with the input line, lowercased. $FILE - which gets replaced with the input line, uppercased. $pre - replaced with the file's base name $ext - replaced with the file's extension. $path - the path from the file name (if any). For example: ls | foreach echo \$File \$FILE \$file will take the output of ls, one file name at a time, and compose and execute a command line made up of the argumets to foreach. In the example, it would echo the filename, the uppercase file name, and the lowercase file name. =head1 OPTIONS -d Skip any input that is a directory. -h Display command line help. -n Do not run the command, just display the command that would be run instead. -p Display pod documentation as a manpage - you need to have pod2man, nroff, and less for this to work. -v Turn on verbosity. Causes diagnostic/debugging information to be displayed. =head1 BUGS No known bugs :) =head1 AUTHORS Kyle R. Burton -- mortis@voicenet.com =head1 SEE ALSO L, L =cut use strict; use vars qw($opt_d $opt_h $opt_n $opt_p $opt_v $VERSION); use Getopt::Std; $VERSION = "0.9.0"; getopts("hnvdp"); if( $opt_h ) { show_usage(); exit(1); } if( $opt_p ) { system("pod2man $0 | nroff -man | less"); exit(0); } my $cmdline = join(' ',@ARGV); print "using cmdline: \"$cmdline\"\n" if $opt_v; my($File, $cmd, $file, $FILE); while( $File = ) { chomp $File; next if $File =~ /^\.{1,2}$/; next if($opt_d && -d $File); my ($path,$name,$pre,$ext) = &parse_file_info($File); $path ||= ''; $name ||= ''; $pre ||= ''; $ext ||= ''; print "path: ",($path||''),"\n" if $opt_v; print "name: ",($name||''),"\n" if $opt_v; print "pre: ",($pre||''),"\n" if $opt_v; print "ext: ",($ext||''),"\n" if $opt_v; $file = lc($File); $FILE = uc($File); # eval "\$cmd = \"$cmdline\""; $cmd = $cmdline; $cmd =~ s/\$file/$file/g; $cmd =~ s/\$File/$File/g; $cmd =~ s/\$FILE/$FILE/g; $cmd =~ s/\$path/$path/g; $cmd =~ s/\$pre/$pre/g; $cmd =~ s/\$ext/$ext/g; print "cmd is: \"$cmd\"\n" if $opt_v; unless( $opt_n ) { print `$cmd`; } } sub show_usage { print "$0 [opts] \n"; print " Options are:\n"; print " -d skip directoires\n"; print " -h display this help screen\n"; print " -v be verbose\n"; print " -n do not execute the command\n"; print " -p display man page\n"; } sub parse_file_info { my $name = shift; my($path,$ext,$file,$pre) = ('','','',''); print "[parse_file_info] parsing: $name\n" if $opt_v; if( $name =~ /^(.*)\/([^\/]+)$/ ) { $path = $1; $file = $2; print "[parse_file_info] found path info: $path $file\n" if $opt_v; } else { $file = $name; print "[parse_file_info] no path info: $file\n" if $opt_v; } if( $file =~ /^(.*)\.([^\.]*)$/ ) { $pre = $1; $ext = $2; print "[parse_file_info] found extension: $pre $ext\n" if $opt_v; } else { print "[parse_file_info] no extension info: $file\n" if $opt_v; } return($path,$file,$pre,$ext); }