2014-09-26 155 views
0

所有Shell命令里面的Perl脚本

我有以下shell命令工作按上壳预期,但不工作里面的perl调用

Shell命令时:

grep -P -s -irl --include \*.v "\s+hello\s?[(].*" <PATH> 

做工精细

Perl内部:

$inst_search = `grep -P -s -irl --include \*.v "\s+$inst\s?[(].*" @plt_dirs`; 

无法正常工作

我怀疑我在grep里面缺少正则表达式的东西。请纠正我!

感谢, 维韦克

+0

检查此...如果这可以帮助您:http://stackoverflow.com/questions/3200801/how-can-i-call-a-shell-command-in-my-perl-script – Praveen 2014-09-26 09:46:48

+0

Perl有一个内置的'grep'。我建议使用它而不是shell转义。 – Sobrique 2014-09-26 09:54:00

+0

@Praveen:它被发现有用,并解决我的问题..谢谢! – 2014-09-28 06:39:55

回答

0

当使用字符串调用exec/system/qx(或反引号)时,Perl将转义特殊的外壳字符。

尝试使用exec或系统函数,但传递一个列表,例如

system('grep', '-P', '-s', '-irl', '--include', '\*.v', '"\s+hello\s?[(].*"', @plt_dirs); 

您可能还想看看为您执行一些错误处理的模块,如IPC :: System :: Simple。

0

试试这个:

$inst_search = qx#grep -P -s -irl --include \*.v "\s+$inst\s?[(].*" @plt_dirs#; 

或为引用使用任何其他非字母数字字符,而不是"#"