0

我正在寻找一种允许 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-> 

然后脚本等待任何进一步的命令,直到可以使用“quit”命令退出。'quit' 会产生以下输出:

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 代码进行哪些修改才能解决此问题?任何帮助将不胜感激......我的命令在命令行上运行良好,所以这是唯一阻止我的应用程序的事情。

4

1 回答 1

0

我对 PHP 无能为力,但是如果您使用“-blx”而不是“-b”调用 grads,那么它将自动以横向模式启动,并在您的命令完成后退出。当 GrADS 询问横向模式时,-l 消除了用户输入的需要,并且 -x 告诉它在完成后退出。您可以添加“c”,后跟命令或脚本的名称。例如:

# /usr/local/bin/grads -blxc 'q config'

将以横向模式启动grads,执行打印出配置信息的命令,然后退出。命令“q config”可以替换为脚本的名称。有关 GrADS 命令行选项的文档位于http://iges.org/grads/gadoc/gradcomdgrads.html

于 2015-11-12T19:13:28.160 回答