Subject: | finding difficulty in fgrep function |
Hello Sir,
I am facing some unusual problem with fgrep function. I have created program file textfind5.pl using some : xyz username.. when i executed it with same account (xyz) it gives me correct result. But when i tried to execute it from anybody else account it gives me error :- #######"Cannot use file '/hm/kiranc/temp.txt' for fgrep: Permission denied at textfind5.pl line 258"############
Kindly help me out in this matter.
my program takes logical expression as an input which contains pattern to be searched and returns the filoe name containing that logical expression
#!/usr/bin/perl
########################################################################################################################################
# Created By Pradeep Kamat on 01-04-2005 (dd-mm-yyyy)
#
# File can be used to search specific pattern in multiple files.
#
########################################################################################################################################
use File::Grep qw(fgrep);
#our $SILENT = 0;
#&searchPatter();
#&getMatch("use");
my @pr_arr=&inputfields("/usr/local/ipman/data/scaninput.dat");
print "\n After Function is called.......... \n\n";
#print "\n@pr_arr :- @pr_arr\n";
foreach (@pr_arr)
{
#print "hieeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\n";
#print "\nBefore searchPatter :- @{$_->{File_List}}";
&searchPatter($_->{File_List},$_->{Logic_exp},$_->{Hash_search});
#print "\n @{$_->{File_List}}";
#print "\n $_->{Logic_exp}";
#print "\n %{$_->{Hash_search}}";
}
sub searchPatter
{
my ($file_list,$log_exp,$hs_arr)= @_;
#print "All files cotaining file :- @{$file_list} \n";
my @post_arr=&createPostfix($file_list,$log_exp,$hs_arr);
my $res = &evalPostfix(@post_arr);
open(FL1,">> /hm/aroras/res.log");
my @key = keys(%{$res});
print FL1 "All files cotaining pattern $log_exp \n";
foreach $val(@key)
{
print FL1 "$val \n";
}
close(FL1);
}
#################################################################
# Created By Pradeep Kamat
# Created On :- 31/03/2005 (dd/mm/yyyy)
# Description :- function takes expression as an input and return
# an array containing postfix form of expression
# Input :- logical expression as string
# Return :- postfix expression as array
#################################################################
sub createPostfix
{
my @file_arr =@{@_[0]};
my $in =@_[1];
my $ele_hash = @_[2];
#print "\n Inside CREATEPOSTFIX Functin File list :- @file_arr \n\n logical expression :- $in \n\n elements are :- %ele_hash\n\n";
my $err_flag=0;
my @post_arr;
my @opr_stk;
my @args = split / /,$in;
my $match_res=0;
#check is argument list starting with special character
my $match_res = $args[0] =~ /^AND|^OR/;
#print "\$match_res :- $match_res \n";
if ($match_res == 1)
{
print "Your pattern should not contain any of the meta-character at the starting position";
$err_flag=1;
return;
}
$cnt = scalar(@args);
$args[$cnt]= ")";
push (@opr_stk, "(");
foreach $val (@args)
{
my $res = $val=~ /AND|OR|\)/;
#print "\$res :- $res \n";
if($res != 1)
{
print "val is :- $val element is :- $ele_hash->{$val} \n";
%temp = &getMatch($ele_hash->{$val},\@file_arr);
push(@post_arr,\%temp);
}
elsif ($val=~ /AND|OR|\(/)
{
print "\$val :- $val \n";
push(@opr_stk,$val);
}
elsif ( $val=~ /\)/ )
{
while ( ($opt = pop(@opr_stk)) ne '(' )
{
#print "\$opt :- $opt \n";
push(@post_arr,$opt);
}
}
}
#print "\n post fix form is :- @opr_stk";
print "\n post fix form is :- @post_arr";
return @post_arr;
}
#################################################################
# Created By Pradeep Kamat
# Created On :- 31/03/2005 (dd/mm/yyyy)
# Description :- function takes postfix expression as an input and # return reference to hash containing result
# Input :- postfix expression as array
# Return :- result of expression hash reference
#################################################################
sub evalPostfix
{
my @par = @_;
print "\n Inside EVALPOSTFIX Function parameters are :- @par\n";
my (@res_stack,%res,$hash_opr1,$hash_opr2);
foreach (@par)
{
if (/AND|OR/)
{
print "Eval function::EvalPostfix in inside if value is :- $_ \n ";
$hash_opr1 =pop(@res_stack);
$hash_opr2 =pop(@res_stack);
%res = (/AND/ eq '1') ? &and_fun($hash_opr2,$hash_opr1) : &or_fun($hash_opr2,$hash_opr1);
push(@res_stack,\%res);
}
else
{
print "Eval function::EvalPostfix in inside else \n";
push(@res_stack,$_);
}
}
return $res_stack[0];
}
#################################################################
# Created By Pradeep Kamat
# Created On :- 31/03/2005 (dd/mm/yyyy)
# Description :- function takes two operand and perform logical
# AND operation on them
# Input :- two operand as hash reference
# Return :- hash containing AND result
#################################################################
sub and_fun
{
#print "\n Inside AND_FUN Functin";
my ($process,$final)= @_;
my %ret_hash;
my @key_val = keys(%{$process});
if (scalar(@key_val) == 0 || scalar(keys(%{$final})) == 0 )
{
#print "\n Leaving AND_FUN Functin22222";
return %ret_hash;
}
foreach $_ (@key_val)
{
if (exists($final->{$_}))
{
#print "\n File Name :- $_ And_fun Result :- $final->{$_}";
$ret_hash{$_}=$process->{$_};
}
}
return %ret_hash;
}
#################################################################
# Created By Pradeep Kamat
# Created On :- 31/03/2005 (dd/mm/yyyy)
# Description :- function takes two operand and perform logical
# OR operation on them
# Input :- two operand as hash reference
# Return :- hash containing OR result
#################################################################
sub or_fun
{
#print "\n Inside OR_FUN Functin";
my ($process,$final)= @_;
my @key_val = keys(%{$process});
foreach $_ (@key_val)
{
if (exists($final->{$_}))
{
$final->{$_}=$process->{$_};
}
}
#print "\n Leaving OR_FUN Functin";
return %{$final};
}
#################################################################
# Created By Pradeep Kamat
# Created On :- 31/03/2005 (dd/mm/yyyy)
# Description :- function takes search string as an input
# and search it in the list of files and returns
# files containing search string and count of
# matches
# Input :- search string
# Return :- hash containing match result
#################################################################
sub getMatch
{
print "\n Inside GETMATCH Function\n";
my $in =@_[0];
my @file_arr =@{@_[1]};
my (@match); #stores output of the search
my %arr_match;
open(FL,"> /hm/aroras/match.log");
#print ("\$in :- $in \n ");
chomp ($in);
print "\$search_ele :-$in" . ": \n";
#@file_arr= glob "/usr/local/share/axyftp/help/*.*";
print "Getmatch::File arrary :- @file_arr\n";
@match = fgrep { /$in/ } @file_arr;
foreach $local (@match)
{
print "File Name :- $local->{filename} count is :- $local->{count}\n\n";
if($local->{count} ne '0')
{
$arr_match{$local->{filename}}=$local->{count};
print "\n function1 called \n";
print FL "pattren :- $search_ele";
print FL "File Name :- ". $local->{filename}. "\n";
print FL "Count :- " . $local->{count}. "\n";
my @key_arr = keys(%{$local->{matches}});
foreach $key_arr (@key_arr)
{
print FL "Matches :- " . $local->{matches}->{$key_arr} . "\n";
print FL "\n";
}
}
}
close(FL);
#open(FL1,"> /hm/aroras/res.log");
return %arr_match;
}
#################################################################
# Created By Sangita Arora And Pradeep Kamat
# Created On :- 04/04/2005
# Description :- Reads the file for dir path and file filter
# Input :- files containing schema
# Return :- void
#################################################################
sub inputfields
{
my (@ret_arr);
$filepath=$_[0];
open(FL,"<$filepath") or die "Cannot open $filePath: $!";
@sehema=<FL>;
for(my $i=0;$i<=$#sehema;$i++)
{
@fields=split(/\|/,$sehema[$i]);
#print ("test:::$sehema[0]");
my $logical_exp;
my %arg_hash;
for($x=4;$x<=$#fields;$x++)
{
if ($x == 4)
{
my @temp_arr = split(/ /,$fields[$x]);
foreach(@temp_arr)
{
if (/AND|OR|\&\&|\|\|/)
{
$logical_exp.= /AND|\&\&/ ? " AND" : " OR";
}
else
{
print "Inside Else\n";
$logical_exp.=$_;
}
}
}
if ($x != 5 && $x != 4)
{
if ( $x % 2 == 0)
{
my $str =$fields[$x+1];
$arg_hash{$fields[$x]} = $str;
#print"inputfields::Hash is :- $arg_hash{$fields[$x]}"
}
}
}
my $dir=$fields[2];
my $filefilter=$fields[3];
my @filelist=filefinder($dir,$filefilter);
#print "File list :- @filelist \n\n";
$ret_arr[$i] = ({
"File_List" => \@filelist,
"Logic_exp" => $logical_exp,
"Hash_search" => \%arg_hash,
});
#print " \n Logical Expression is :- $logical_exp\n";
}
close(FL);
return @ret_arr;
}
#################################################################
# Created By Sangita Arora
# Created On :- 04/04/2005
# Description :- Fetch files from the given directory depending upon filter
# Input :- file path and file filter
# Return :- fully qulified file name array
#################################################################
sub filefinder
{
$dirinput=$_[0];
$filefilterinput=$_[1];
print "\nfind $dirinput -name $filefilterinput";
$str=`find $dirinput -name "$filefilterinput" `;
@array=split(/\n/,$str);
for (my $i=0;$i<=$#array;$i++)
{
chomp($array[$i]);
print "$i:$array[i]::\n";
$mystr=$array[$i];
@temp=~split(/:/,$mystr);
print("Match") if($temp[2]=~m/Permission/);
}
print("\n\n\nendarray");
return @array;
}