我正在尋找一種允許PHP腳本在提示時發送多個命令的解決方案。當下面的代碼是從shell執行:等待命令行輸入到來自PHP的shell腳本
[email protected] [~]# /usr/local/bin/grads-2.0.2/bin/grads -b
此輸出結果:
Grid Analysis and Display System (GrADS) Version 2.0.2
Copyright (c) 1988-2011 by Brian Doty and the
Institute for Global Environment and Society (IGES)
GrADS comes with ABSOLUTELY NO WARRANTY
See file COPYRIGHT for more information
Config: v2.0.2 little-endian readline printim grib2 netcdf hdf4-sds hdf5 opendap-grids,stn geotiff shapefile
Issue 'q config' command for more detailed configuration information
Landscape mode? ('n' for portrait):
正如你所看到的,劇本正在等待Y/N輸入。當y/n被輸入時,輸出結果如下:
Landscape mode? ('n' for portrait): y
GX Package Initialization: Size = 11 8.5
Running in Batch mode
ga->
然後,該腳本用於任何進一步的命令等待,直到它可以用「退出」命令來退出。 「退出」在輸出結果如下:然而
ga-> quit
No hardcopy metafile open
GX package terminated
[email protected] [~]#
我的問題出現時,我試圖通過PHP來做到這一點。我的代碼如下:
$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
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);
$cwd = '/';
$process = proc_open('/usr/local/bin/grads-2.0.2/bin/grads -b', $descriptorspec, $pipes, $cwd);
if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// Any error output will be appended to /tmp/error-output.txt
fwrite($pipes[0], 'y');
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
}
然而,這是所有輸出一次:
Grid Analysis and Display System (GrADS) Version 2.0.2
Copyright (c) 1988-2011 by Brian Doty and the
Institute for Global Environment and Society (IGES)
GrADS comes with ABSOLUTELY NO WARRANTY
See file COPYRIGHT for more information
Config: v2.0.2 little-endian readline printim grib2 netcdf hdf4-sds hdf5 opendap-grids,stn geotiff shapefile
Issue 'q config' command for more detailed configuration information
Landscape mode? ('n' for portrait): GX Package Initialization: Size = 11 8.5
Running in Batch mode
ga-> [EOF]
No hardcopy metafile open
GX package terminated
正如你所看到的,腳本,而無需等待輸入一起賽跑......什麼做修改我需要對PHP代碼進行修復才能解決這個問題?任何幫助將不勝感激...我的命令在命令行工作正常,所以這是唯一的東西拿回我的應用程序。
可能的重複:http://stackoverflow.com/q/5794030 –
不是真的,該用戶想創建一個文字'PHP外殼',而我正在尋找只需從PHP腳本中使用真正的unix shell。 –
您可以嘗試使用PHP的expect模塊http://php.net/manual/en/expect.examples-usage.php – piotrekkr