Answers exec ()

Let's take this command, for example:

$command = "echo '/just/for/the/test /heh/' | awk '//just// {print $1}'"; 

When directly copying inside the shell, I get the following error:

 awk: cmd. line:1: //just// {print $1} awk: cmd. line:1: ^ unterminated regexp 

But, when I exec() it, I get status code 1 without output:

 exec($command, $output, $status); var_dump( $command, $output, $status ); // string(69) "echo '/just/for/the/test /heh/' | awk '//just// {print $1}'" // array(0) { } // int(1) 

How to get the STDERR exec part?

+4
source share
2 answers

You have to redirect stderr to stdout anyway like this

 $stout = exec($command . " 2>&1", $output, $status); 

See also here PHP StdErr after Exec ()

+5
source

with exec() only way I can think of is to redirect STDERR to STDOUT in an executable command, for example:

 $command = "echo '/just/for/the/test /heh/' | awk '//just// {print $1}' 2>&1"; 

An alternative to exec() is to use the proc_open() family of functions. Using proc_open() you execute the command and open the file pointers to STDIN, STDOUT and STDERR, from which you can read / write

see manual for more information

+3
source

Source: https://habr.com/ru/post/1434781/


All Articles