通过PHP执行shell命令并获取有关是否来自stdout和stderr的输出

我想执行一个外壳命令,并且具有以阵列,使得输出:\ 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>

展开原文

原文

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.

duanjianhe1388
duanjianhe1388 那么PHP脚本本身是在STDOUT写一些行,有些是STDERR?你能修改PHP脚本吗?如果没有,你可以使用Bash重定向(如果你澄清,我会发布示例代码)。此外,这些行是按任何顺序排序的吗?
大约一年之前 回复

1个回答



如果你在字符串输入中加入换行符并通过它运行它:</ p>

   $ arr = explode(PHP_EOL,$ output); 
</ code> </ pre>

然后$ arr会让你的数组每个$ output行有一个项目。</ 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.

duan33360
duan33360 遗憾的是,错过了区分stdout和stderr的更重要的部分
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐