php 从 shell_exec() 获取输出和退出状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1941755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting output and exit status from shell_exec()
提问by Tony Miller
When doing something like
当做类似的事情时
$output = shell_exec("command 2>&1");
collecting the command's stdout & stderr in $output, is there a way to find the command's exit status?
在 中收集命令的标准输出和标准错误$output,有没有办法找到命令的退出状态?
One could write the command output to a temp file and then append the exit status, but that's rather clunky. Any better suggestions?
可以将命令输出写入临时文件,然后附加退出状态,但这相当笨拙。有什么更好的建议吗?
回答by Tony Miller
As you've already seen, when using shell_exec you have to chain your "real" command with echo $? to get the exit status:
正如您已经看到的,在使用 shell_exec 时,您必须使用 echo $? 获取退出状态:
$output_including_status = shell_exec("command 2>&1; echo $?");
but if you want the clean way, then you want to use the execfunction, which allows a 3rd agument explicitly for this purpose.
但是如果你想要干净的方式,那么你想要使用exec函数,它允许明确地为此目的使用第三个参数。
回答by Luká? Lalinsky
回答by Robert Sinclair
Following worked for me with exec() to show the output
以下使用 exec() 为我工作以显示输出
exec(your_command, $output, $return_var);
var_dump($output);
var_dump($return_var);

