我可以传回执行脚本的输出,但是如果脚本出错,则不会输出错误。

// This is a file that doesn't exists, for testing
$command = './path/to/non/existing/script.sh';

$commandOutput = exec($command, $commandOutput); // works but no error output
//passthru($command, $commandOutput); // works but error output was 127 not file not found
//$commandOutput = escapeshellcmd($command);
echo "The Output:\n|".$commandOutput."|\n";
var_dump($commandOutput);

The Output:

||

我想要错误消息的输出:
The Output:

|file not found|

如何或什么功能/参数将执行此操作?

最佳答案

您可以将stderr重定向到stdout,以便exec()等通过将2>&1附加到命令来获取错误消息。

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html

10-08 17:18