bash in perl issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash in perl issue
# 1  
Old 08-26-2011
bash in perl issue

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

Code:
[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.

Code:
`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";

# 2  
Old 08-26-2011
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()
# 3  
Old 08-27-2011
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:

Code:
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

Last edited by durden_tyler; 08-27-2011 at 12:04 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare bash arrays issue

Hello everyone, I need help comparing 2 arrays. the first array is static; the second array is not .. array1=( "macOS Mojave" "iTunes" ) cd /Volumes array2=( * ) # output of array2 macOS Mojave iTunes Mac me The problem occurs when I compare the arrays with the following code - ... (6 Replies)
Discussion started by: trexthurman
6 Replies

2. Shell Programming and Scripting

Bash arithmetic issue

I found the following issue by simply increasing a variable. The ((A++)) expression returns an error, the other expressions both return 0. Does anyone know why? script.sh: #! /bin/bash A=0 B=0 C=0 ((A++)) ; echo "${?}" ((B=B+1)) ; echo "${?}" ((C+=1))... (8 Replies)
Discussion started by: elbrand
8 Replies

3. Shell Programming and Scripting

Array Issue In Bash

Hi, I have the following code which is giving error mentioned below. Please can you support on this. Also suggest how can we access all the items against single vasservicename in circlename array,i.e, vasservicename say MTSTV will be available to all circles which are mentioned in the array... (2 Replies)
Discussion started by: siramitsharma
2 Replies

4. Shell Programming and Scripting

Bash script and remote server issue

Hello, I'm attempting to run a script on a remote server via SSH but am having issues getting the script to run using proprietary binaries located on the remote server as it keeps complaining that commands are invalid on the local machine. If I run the script locally on the remote server, it... (2 Replies)
Discussion started by: shadyuk
2 Replies

5. Shell Programming and Scripting

bash ls command file issue

ls -l /md01/EL/MarketData/inbound/ststr/INVENTORY* |tail -5 |awk '{ print $5,$6,$7,$8,$9 }'If I run the above from the command line the output to md_email is formatted correctly as 78213497 May 1 12:50 /md01/EL/MarketData/inbound/ststr/INVENTORY.20120430.PINESTREET.CSV.done 77904740 May 2... (3 Replies)
Discussion started by: smenago
3 Replies

6. Shell Programming and Scripting

variable issue in a bash in a shell script

Hi, I am trying to write a PBS shell script that launches a bash process. The issue is that the bash process needs a variable in it and the shell script is interpreting the variable. How do I pass this as a literal string? Here is my code snippit: TMP=".fasta" FILEOUT=$FILE$TMP cd... (2 Replies)
Discussion started by: bioBob
2 Replies

7. Shell Programming and Scripting

bash integer & string issue

Hi guys, I need for the bash code below a little bit help: cat script.sh #!/bin/bash 5_MYVALUE="test" echo "$5_MYVALUE" If I try to run the script, got follow failure: ./script.sh ./script.sh: line 4: 5_MYVALUE=test: command not found _MYVALUE My questions are how... (4 Replies)
Discussion started by: research3
4 Replies

8. Shell Programming and Scripting

Bash Calculator issue

Hello, I'm relatively new to using bc so I could use some help. In this script im working on I want to have the bc function to calculate float numbers for imagemagicks convert charcoal. Below is what I'm talking about. There are no syntax errors but when it outputs the users frames for example 0-10... (2 Replies)
Discussion started by: jsells20
2 Replies

9. Shell Programming and Scripting

bash version or string issue

Hi all, I'm not sure but I guess, that is a bash version issue. The script working fine on "GNU bash, version 3.2.25(1)-release Ubuntu". #!/bin/bash while IFS=">" read a id val do if ] then VAL=${id%<*}; ID=${id#*</} echo $VAL echo $ID sed... (5 Replies)
Discussion started by: research3
5 Replies

10. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies
Login or Register to Ask a Question