0

I work on windows 7. I try to execute the Gnuwin32 grep command from within a PHP script like so var_dump(shell_exec("grep")), so I can see if it works or not. Instead of the expected output of usage: and things like that I get null.

  • C:\gunwin\bin is on my Windows path and on the PHP include_path
  • grep works from the command line
  • shell_exec('dir') (or any other 'regular' windows-environment command) works as expected

I'm sure I'm missing something obvious here, but I can not find out what it is. Does anyone have a suggestion?

EDIT: something strange happened. I tried shell_exec('wget'), just for the sake of it. This works as expected. After that, I tried shell_exec('grep --help') and this actually returns the output I expected. I'm a little confused now.

Dirk McQuickly
  • 2,099
  • 1
  • 17
  • 19

1 Answers1

1

grep, without arguments, writes nothing in stdout, but show two lines (instructing to use --help) on stderr, which is not returned with shell_exec.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • +1 for the explanation. Could you also tell me why this is not the case with `wget`? Is there some inconsistency going on here? – Dirk McQuickly Nov 18 '13 at 14:39
  • Not sure if inconsistency, but sure a different design around different work modes. While wget can use stdin or stdout for input/output of information, the "normal" usage is get parameters as arguments and output to files, so output to console is ok. "usual" usage of grep implies reading from stdin and output to stdout (but can operate the same as wget), so output to stderr to show information seems logical to not interfere with data (just an opinion). – MC ND Nov 18 '13 at 16:23