4 ###APPDATE: 2008-08-27 12:46:27
6 ###APPDESC: extend find to support "-havename,-copy,-move,-from-file"
7 ###APPUSAGE: r-find [Options]
9 ###APPOPTION: -havename [text]:filename match *text*|-copy [Dir]:copy to dir|-move [Dir]:move to dir|-from-file (filename):read options from file
12 #ENV variable MUST be defined somewhere,
13 #FOR perl to search modules from,
15 use lib
$ENV{XR_PERL_MODULE_DIR
};
17 use MyPlace
::Script
::Usage qw
/help_required help_even_empty/;
18 exit 0 if(help_required
($0,@ARGV));
19 #exit 0 if(help_even_empty($0,@ARGV));
21 my $opt_exp = qr/^(-havename|-copy|-move|-from-file)$/;
22 my $file_exp = qr/^-from-file$/;
23 my $name_exp = qr/^-havename$/;
24 my $action_exp = qr/^(-copy|-move)$/;
26 "--copy"=>["-exec","cp","-v","--","{}","NAME_ACTION_DEST",";"],
27 "--move"=>["-exec","mv","-v","--","{}","NAME_ACTION_DEST",";"]
36 #print STDERR "NAMES = ",join(" ",@names),"\n";
37 if($opt_key =~ $name_exp) {
38 push @result,"-name","*$opt*";
40 elsif($opt_key =~ $action_exp) {
43 $name_dest = shift @names;
45 elsif($opt =~ m/^-/) {
46 $name_dest = shift @names;
48 $name_dest = shift @names unless($name_dest);
49 #print STDERR "Name_DEST = ",$name_dest,"\n";
50 die("No destionation found for $opt_key...\n") unless($name_dest);
51 mkdir $name_dest unless(-d
$name_dest);
52 foreach my $action (@
{$actions{$opt_key}}) {
54 $para =~ s/NAME_ACTION_DEST/$name_dest/g;
57 push @result,$opt if($opt and $opt =~ m/^-/);
60 push @result,$opt_key,$opt;
62 #print STDERR ("key=$opt_key\tvalue=$opt\nresult=",join(" ",@result),"\n");
66 sub collect_names
(@
) {
69 foreach my $opt (@_) {
75 if($opt =~ $name_exp) {
87 foreach my $opt (@_) {
89 if($opt_key =~ $action_exp and $opt =~ $opt_exp) {
90 push @
{$result{opt
}},&convert_opt
($opt_key,"");
93 elsif($opt_key =~ $file_exp) {
94 push @
{$result{files
}},$opt;
98 push @
{$result{opt
}},&convert_opt
($opt_key,$opt);
103 if($opt =~ $opt_exp) {
107 push @
{$result{opt
}},$opt;
112 push @
{$result{opt
}},&convert_opt
($opt_key,"");
117 print STDERR
join(" ","find",@_),"\n";
123 # print STDERR "Process :",join(" ",@PARGS),"\n";
125 @names = &collect_names
(@PARGS);
126 my $options = &collect_opt
(@PARGS);
127 my @find_opt = $options->{opt
} ? @
{$options->{opt
}} : ();
128 # print STDERR "Find Options:",join(" ",@find_opt),"\n";
129 my @files = $options->{files
} ? @
{$options->{files
}} : ();
131 foreach my $file(@files) {
133 print STDERR
"File not exists:$file...\n";
136 if(!open FI
,"<",$file) {
137 print STDERR
("File not readable:$file...\n");
140 print STDERR
"From file : $file\n";
145 my @args = split(" ",$_);
146 &process
(@find_opt,@args);
151 &run_find
(@find_opt);