The reason it's hanging is because you forgot to use chomp
after reading from FILE
. So there's a newline at the end of $_
, and it's executing two shell commands:
grep -r $_
Satheesh > out.txt
Since there's no filename argument to grep
, it's reading from standard input, i.e. the terminal. If you type Ctl-d when it hangs, you'll then get an error message telling you that there's no Satheesh
command.
Also, since you're redirecting the output of grep
to out.txt
, nothing gets put in $result
. If you want to capture the output in a variable and also put it into the file, you can use the tee
command.
Here's the fix:
while (<FILE>) {
print " $_";
chomp;
#This is what expected.
#our $result=`grep -r Unable Satheesh > out.txt`;
our $result=`grep -r $_ Satheesh | tee out.txt`;
print $result
}