dousi1994 2014-12-11 08:27
浏览 78
已采纳

php shell_exec正则表达式PCRE / POSIX

I have a problem with my php-script when I call shell_exec and pass a regular expression.

PHP code :

shell_exec("sh myscript.sh 'FOO\s*ONE'");

myscript.sh :

result=$(grep -c "${1}"  myLongFile.txt)
echo ${result}

Like this, it returns 0 but if il call directly with cmd grep -c "FOO\s*ONE" myLongFile.txt it returns 23.

And if i replace in my php script \s by the class [[:space:]] it's working, but I have to use \s

I tried many solutions but failed.

  • 写回答

4条回答 默认 最新

  • duandie0921 2014-12-11 08:55
    关注

    Thank you all,

    I find the solution, i just add option -P for grep in my script like this :

    result=$(grep -P -c "${1}"  myLongFile.txt)
    

    Now, i can use \s

    -P, --perl-regexp Interpret PATTERN as a Perl regular expression.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?