我正在寻找一种解决方案,允许PHP脚本在出现提示时发送多个命令。从shell执行以下代码时:
root@host [~]# /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
root@host [~]#
当我尝试通过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代码进行哪些修改才能解决此问题?任何帮助将不胜感激...我的命令在命令行工作正常,所以这是我的应用程序的唯一回复。
答案 0 :(得分:0)
我无法帮助PHP,但是如果你使用“-blx”而不是“-b”调用grads,那么它将自动以横向模式启动,并在命令完成时退出。当GrADS询问Landscape模式时,-l消除了用户输入的需要,-x告诉它在完成时退出。您可以添加'c',后跟命令或脚本的名称。例如:
# /usr/local/bin/grads -blxc 'q config'
将以横向模式启动Grads,执行打印配置信息的命令,然后退出。命令'q config'可以替换为脚本的名称。有关GrADS命令行选项的文档位于http://iges.org/grads/gadoc/gradcomdgrads.html