简体   繁体   中英

Executing grep in perl always returns and empty string

When I run grep from my command line in CentOS 5.8 using the following command I get

grep -E "APPLIANCE=\"VPMX\"" /filepath/appliance_data.sh

with the results of

APPLIANCE="VPMX"; export APPLIANCE

When I run the following commands perl

$out = `grep -E "APPLIANCE=\"VPMX\"" /filepath/appliance_data.sh`;
`echo "Output grep TV: $out" >> /tmp/debug`;

the $out variable is always an empty string

How do I get the same output as the command line grep?

I have tried quiet grep using the -q parameter, and have also tried the command with < /dev/null with no change in the result.

You need to escape your backslashes as backticks work like a double quoted string by default:

$out = `grep -E "APPLIANCE=\\"VPMX\\"" /filepath/appliance_data.sh`;

Alternatively, you could the single quoted form of qx :

$out = qx'grep -E "APPLIANCE=\"VPMX\"" /filepath/appliance_data.sh';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM