从PHP编写到stdin? [英] Writing to stdin from PHP?

查看:127
本文介绍了从PHP编写到stdin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux中,我想从PHP运行gnome zenity进度栏窗口. zenity的工作原理是这样的:

In linux I want to run a gnome zenity progress bar window from PHP. How zenity works is like this:

linux-shell$ zenity --display 0:1 --progress --text='Backing up' --percentage=0
10
50
100

因此,第一个命令将以0%的速度打开zenity进度条.然后,Zenity将标准输入数字作为进度条百分比(因此,当您键入这些数字时,它将从10%增至50%到100%).

So the first command opens the zenity progress bar at 0 percent. Zenity then takes standard input numbers as the progress bar percentage (so it will go from 10% to 50% to 100% when you type those numbers in).

我想不通如何使PHP键入这些数字,

I can't figure out how to get PHP to type in those numbers though, I have tried:

exec($cmd);
echo 10;
echo 50;

并且:

$handle = popen( $cmd, 'w' );
fwrite( $handle, 10 );

并且:

$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w")  // stdout is a pipe that the child will write to
);

$h = proc_open($cmd, $descriptorspec, $pipes);

fwrite($pipes[1], 10);

但是它们都不更新进度条.我可以通过哪种方式模仿stdin在linux shell上的作用,以使zenity更新其进度条?

But none of them updates the progress bar. In what way can I mimic the effect of the stdin on the linux shell to get zenity to update its progress bar?

推荐答案

您首先使用当前脚本的stdin(而不是您提供的文本)的副本执行命令.

Your first executes the command with a copy of the current script's stdin, not the text you provide.

您的第二个失败是因为您忘记了换行符.请尝试使用fwrite($handle, "10\n").请注意,达到EOF时,禅宗性似乎会跳升至100%(例如,在PHP脚本末尾隐式关闭$handle).

Your second fails because you are forgetting the newline. Try fwrite($handle, "10\n") instead. Note that zenity seems to jump to 100% when EOF is reached (e.g. by the implicit close of $handle at the end of your PHP script).

您的第三个失败,因为您忘记了换行符,并且正在写入错误的管道.尝试使用fwrite($pipes[0], "10\n"),并记住与上述相同的有关EOF的注释.

Your third fails because you are forgetting the newline and you are writing to the wrong pipe. Try fwrite($pipes[0], "10\n") instead, and remember the same note regarding EOF as above.

这篇关于从PHP编写到stdin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆