通过PHP执行shell命令并获取有关是否来自stdout和stderr的输出
I’d like to execute a shell command and have the output in an array such that: (1) each line of the output is an element in the array (2) the lines are “tagged” as whether from stdout or stderr
Something like:
╔═════════╦══════════════════╗
║ Line No ║ Stdout or stderr ║
╠═════════╬══════════════════╣
║ Line1 ║ Stdout ║
╠═════════╬══════════════════╣
║ Line2 ║ Stdout ║
╠═════════╬══════════════════╣
║ Line3 ║ Stderr ║
╠═════════╬══════════════════╣
║ Line4 ║ Stdout ║
╚═════════╩══════════════════╝
This is a table, but what I really meant was a multi-dimensional array where $thisarray[0], $thisarray[1] are rows. I am assuming that the shell command executed will always generate a new line before it switches to either stderr or stdout.
This way, I'd be able to see which line belongs to which file descriptor, and be able to see it in context.
Is there any way to do this in PHP, with the shell exec commands or otherwise?
If this is not possible with PHP, I do not mind other technologies.
我想执行一个外壳命令,并且具有以阵列,使得输出:\ N(1) 输出的每一行是在该阵列\ N(2)行被“标记”为是否从stdout或stderr p>
东西等的元件: p>
╔═════════════════════════════════════════════════ ════════╬══════════════════╣
║一号线║║标准输出
╠═════════╬══ ════════════════╣
║2号线║║标准输出
╠═════════╬═════════════ ═════╣
║Line3║Stderr║
╠═══════════════════════════════════╣║║║║║║44║║ 标准差║
╚══════════════════════════════════════════════════════════════════════════════════════════ ═══════════╝
代码> PRE>
这是一个表,但是我真正的意思是一个多维阵列,其中$ thisarray [0 ],$ thisarray [1]是行。
我假设执行的shell命令在切换到stderr或stdout之前总会生成一个新行。 p>
这样,我' d是能够看到哪一行属于哪个文件描述符,并能够看到它在上下文 p>
是否有任何的方式来做到这在PHP,与壳EXEC指令或以其它方式 ? p>
如果PHP无法做到这一点,我不介意其他技术。 p>
div>
If you take your string input with line breaks in it and run it through this:
$arr = explode(PHP_EOL, $output);
Then $arr will have your array with one item per $output line.