2011-03-26 49 views
6

在linux中,我想從PHP運行一個gnome zenity進度條窗口。如何zenity的工作原理是這樣的:從PHP寫入stdin?

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

所以第一個命令在0%的打開zenity進度條。然後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); 

但是他們都沒有更新進度條。以什麼方式可以模仿stdin在linux shell上的效果以獲得更新其進度條的靈活性?

回答

6

您的第一個命令將執行當前腳本的stdin副本,而不是您提供的文本。

你的第二次失敗,因爲你忘記了換行符。改爲嘗試fwrite($handle, "10\n")。請注意,當達到EOF時,zenity似乎會跳到100%(例如,通過在PHP腳本末尾隱式關閉$handle)。

你的第三次失敗是因爲你忘記了換行符而且你正在寫入錯誤的管道。改爲嘗試fwrite($pipes[0], "10\n"),並記住與上述EOF相同的註釋。

+0

非常感謝!不能相信我忘了換行符! :) – hamstar 2011-03-26 22:04:50