简体   繁体   中英

Underscore in php shell_exec

i try to execute a grep command inside a php shell_exec. And it works fine besides it fails when i have a underscore in the search word. I can not seem to figure out why this fails because of a underscore since, a grep command with a underscore in search word works in shell code below:

$output = shell_exec("grep -l -r '$search_word'"); 

The content in search_word variable is dynamic from database but the word that gives me trouble is base_64

Before PHP spawns a subprocess your command will be $search_word evaluated:

grep -l -r '....'
# So in $search_word is set to `john doe` it will become:
grep -l -r 'john doe'

How PHP behaves I'm not sure, it might be stalling waiting for the process to finish, it might have been closing stdin already.

Your above command will expect input from stdin because no file name is specified, breakdown:

grep [option]... [pattern] [file]...
-l will only print file name of the matched file
-r recursive search.

TLDR: You properly want to specify a file / directory to search in:

$output = shell_exec("grep -l -r '$search_word' ."); 
// Or maybe
$output = shell_exec("grep -l -r '${search}_word' ."); # will use $search variable as an input from PHP while _word is a string now.

试试这样: $output = shell_exec("grep -l -r '$search_word' ./*");

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