4 ###APPDATE: 2008-08-27 12:46:27
6 ###APPDESC: test for CONDITIONS, if true perform ACTIONS on testing output
7 ###APPUSAGE: [Options] -if (COND) [-do (ACTION)]
8 ###APPEXAMPLE: r-test -key .c -if find /src/ -do echo
9 ###APPOPTION: -key (key):the key to test|-from-file (file):read options from file|-if (COND):support "find" "grep"|-do (ACTION):buildin actions are "rm" "mv" "cp" "echo"|-dest [text]:append text as arguments for action
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 #use MyPlace::Script::Debug qw/dump_var/;
19 exit 0 if(help_required
($0,@ARGV));
20 #exit 0 if(help_even_empty($0,@ARGV));
22 my $option_exp = qr/^(:?-key|-if|-do|-from-file|-dest)$/;
23 my $mul_exp = qr/^(:?-key|-from-file|-dest)/;
24 my $alias_exp = qr/^(:?-find|-grep|-move|-copy|-delete|-echo)$/;
26 "-find"=>["-if","find"],
27 "-grep"=>["-if","grep"],
28 "-move"=>["-do","mv"],
29 "-copy"=>["-do","cp"],
30 "-echo"=>["-do","echo"]
40 foreach my $opt(@args) {
42 if($opt =~ $option_exp) {
44 @
{$result{$opt}}=() unless($opt =~ $mul_exp);
46 elsif($opt =~ $alias_exp) {
51 push @
{$result{$opt_key}},$opt;
55 if($opt =~ $option_exp) {
57 if($opt !~ $mul_exp) {
61 elsif ($opt =~ $alias_exp) {
62 ($opt_key,$opt) = @
{$alias{$opt}};
66 push @
{$result{"default"}},$opt;
74 my @keys = @
{ shift @_};
75 my @conds = @
{ shift @_};
76 my @actions = @
{ shift @_ };
77 my @dests = @
{ shift @_ };
79 my $action = @actions ?
shift @actions : "echo";
80 my $test = @conds ?
shift @conds : "find";
84 if($action eq "mv" or $action eq "cp") {
85 my $dstdir = shift @actions;
86 $dstdir = shift @dests if(@dests and !$dstdir);
87 $dstdir = join("_",@keys) unless($dstdir);
88 mkdir $dstdir unless(-d
$dstdir);
89 @prev_act = ($action,"-v","--");
90 @post_act = ($dstdir);
92 elsif($action eq "rm") {
93 @prev_act = ("rm","-v","--");
95 elsif($action eq "rmdir") {
96 @prev_act = ("rm","-fdr");
99 push @prev_act,$action;
114 push @post_act,@dests;
116 if($test eq "find") {
118 push @conds,"-iname","*$_*","-or";
120 pop @conds if(@keys);
122 elsif($test eq "grep") {
126 $pattern = $pattern . "|$_";
132 push @conds,"-H","-q",$pattern;
134 print STDERR
"Key:",join(" ",@keys);
135 my @test_result = ();
136 unless(open FI
,"-|",$test,@conds) {
137 print STDERR
"\nCan't fork $test ",join(" ",@conds),"\n";
142 push @test_result,$_ if($_);
145 print STDERR
" (",$#{test_result} + 1," matched)\n";
146 system(@prev_act,$_,@post_act) foreach(@test_result);
151 my $options = &getopt
(@PARGS);
152 my @keys = $options->{"-key"} ? @
{$options->{"-key"}} : ();
153 my @conds = $options->{"-if"} ? @
{$options->{"-if"}} : ();
154 my @actions = $options->{"-do"} ? @
{$options->{"-do"}} : ("echo");
155 my @files = $options->{"-from-file"} ? @
{$options->{"-from-file"}} : ();
156 my @dests = $options->{"-dest"} ? @
{$options->{"-dest"}} : ();
157 #dump_var(\@_,$options,\@keys,\@conds,\@actions,\@dests,\@files);
160 foreach my $file(@files) {
161 $file="/dev/stdin" if($file eq "-");
163 print STDERR
"File not exists:$file...\n";
166 if(!open FI
,"<",$file) {
167 print STDERR
("File not readable:$file...\n");
170 print STDERR
"From file : $file\n";
175 my @args = split(" ",$_);
176 &process
("-key",@keys,"-if",@conds,"-do",@actions,"-dest",@dests,@args) if(@args);
181 &do_if_key
(\
@keys,\
@conds,\
@actions,\
@dests);