bash in perl issue

I use the following shell script in bash and it works fine. It returns 1

[root@server ]# cat /etc/httpd/conf/res.txt
maldet(24444): {scan} scan completed on eicar_com.zip: files 1, malware hits 1, cleaned hits 0

[root@server ]# if [[ "$(cat /etc/httpd/conf/res.txt| grep -E -o -m 1  'malware hits 0')" ]]; then  echo 0 > /etc/httpd/conf/malflag; else echo 1 > /etc/httpd/conf/malflag; fi;cat /etc/httpd/conf/malflag
1

When I use this in the perl script it always returns 0. ie the variable returns 0 and not 1. Please help me.

`if [[ "$(cat /etc/httpd/conf/res.txt| grep -E -o -m 1  'malware hits 0')" ]]; then  echo 0 > /etc/httpd/conf/malflag; else echo 1 > /etc/httpd/conf/malflag; fi`;

$malinput = `cat /etc/httpd/conf/malflag`;
print "malinput:  $malinput \n";

why are you embedding a shell script within Perl? use native Perl code or don't use Perl at all. work smarter; not harder.

try wrapping in system()

I agree with frank_rizzo. Perl can do everything that you are doing with that Bash script, so if you do want to use Perl, use its inbuilt pattern-matching capabilities to process your file.

Maybe something like this:

perl -lne 'BEGIN {$malflag = 1} if (/malware hits 0/) {$malflag = "0"; exit} END {print $malflag}' /etc/httpd/conf/res.txt

The output was not printed to the "malflag" file because the file appeared to be created only so you could read it back; we have $malflag for that same purpose.
If you do want the malflag file, then you could either redirect the output of the one-liner, or write a Perl program that opens res.txt for reading and malflag for writing.

As for why the embedded Bash script inside the Perl program returns 0 in all cases - that's due to the special way Perl treats an interpolated string to be run as a system command.

tyler_durden