3 # Perform multiple moves/copies.
5 # Usage: multi mv 's/$/.old/' *.c
6 # multi cp 's:/tmp/(.*):$1:' *
7 # multi - svn mv - 's/foo/bar/' *foo*
9 # Options: -n don't actually do anything, just print what it would do
10 # -r reverse the arguments to each command
11 # -q don't print the commands as they are executed
13 # Note that all variables in this script begin with a double
14 # underscore. This is because the <action> parameter is a piece of
15 # user-supplied Perl, which might perfectly legitimately want to
16 # define and use its own variables in the course of doing a complex
17 # transformation on file names. Thus, I arrange that all _my_
18 # variables stay as far out of its likely namespace as they can.
20 $__quiet = $__donothing = $__reverse = 0;
22 while ($ARGV[0] =~ /^-(.+)$/) {
24 $__quiet = 1, next if $1 eq "q";
25 $__quiet = 0, $__donothing = 1, next if $1 eq "n";
26 $__reverse = 1, next if $1 eq "r";
29 die "usage: multi <cmd> <action> <files>\n" .
30 " e.g. multi mv 'tr/A-Z/a-z/' *\n" if $#ARGV < 2;
31 " also: multi - <multiple-word-cmd> - <action> <files>\n" .
32 " or multi - svn mv - 'tr/A-Z/a-z/' *\n" if $#ARGV < 2;
35 if ($ARGV[0] eq "-") {
37 while (defined $ARGV[0] and $ARGV[0] ne "-") {
38 push @__cmd, shift @ARGV;
40 if (!defined $ARGV[0]) {
41 die "multi: no terminating - in multiple-word command mode\n";
43 shift @ARGV; # eat trailing -
45 $__cmd[0] = shift @ARGV;
47 $__action = shift @ARGV;
50 $_ = $__origfile = shift @ARGV;
53 ($__origfile, $__newfile) = ($__newfile, $__origfile) if $__reverse;
54 &pcmd
(@__cmd, $__origfile, $__newfile) if !$__quiet;
55 system @__cmd, $__origfile, $__newfile if !$__donothing;
60 printf "%s\n", join " ", map { &fmt
($_) } @words;
65 if (/[ !"#$&'()*;<>?\\`|~]/) {