从 PHP 写入标准输入?

发布于 2024-10-26 12:26:56 字数 831 浏览 4 评论 0原文

在 Linux 中,我想从 PHP 运行 gnome zenity 进度条窗口。 zenity 的工作原理如下:

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

因此第一个命令将 zenity 进度条打开为 0%。然后 Zenity 将标准输入数字作为进度条百分比(因此当您输入这些数字时,进度条将从 10% 变为 50% 再到 100%)。

我不知道如何让 PHP 输入这些数字,但我已经尝试过:

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);

但它们都没有更新进度条。我可以通过什么方式模仿 Linux shell 上 stdin 的效果来让 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

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).

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

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

And:

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

And:

$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);

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来世叙缘 2024-11-02 12:26:56

您首先使用当前脚本的标准输入副本(而不是您提供的文本)执行命令。

你的第二个失败是因为你忘记了换行符。尝试使用 fwrite($handle, "10\n") 代替。请注意,当达到 EOF 时,zenity 似乎会跳到 100%(例如,通过在 PHP 脚本末尾隐式关闭 $handle)。

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

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

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).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文