0

I am looking for a solution that allows a PHP script to send multiple commands when prompted. When the following code is executed from the shell:

root@host [~]# /usr/local/bin/grads-2.0.2/bin/grads -b

This output results:

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

As you can see, the script is waiting for y/n input. When y/n is typed, the following output results:

Landscape mode? ('n' for portrait):  y
GX Package Initialization: Size = 11 8.5 
Running in Batch mode
ga-> 

The script then waits for any further commands until it can be quit with the 'quit' command. 'quit' results in the following output:

ga-> quit
No hardcopy metafile open
GX package terminated 
root@host [~]# 

My issue arises however when I try to do this through PHP. My code is as follows:

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

However, this is the output all at once:

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 

As you can see, the script is racing along without waiting for input...what modifications do I need to make to the PHP code to fix this? Any help would be greatly appreciated...my commands work fine at the command line so this is the only thing holding my application back.

B. Schmidt
  • 85
  • 5
  • Possible duplicate of: http://stackoverflow.com/q/5794030 –  Dec 31 '14 at 06:57
  • Not really, that user wants to create a literal 'PHP shell' whereas I'm looking to simply use the real unix shell just from a PHP script. – B. Schmidt Dec 31 '14 at 07:41
  • You could try using expect module for PHP http://php.net/manual/en/expect.examples-usage.php – piotrekkr Dec 31 '14 at 11:26

1 Answers1

0

I can't help with the PHP, but if you invoke grads with "-blx" instead of "-b" then it will start in landscape mode automatically, and quit when your command is finished. The -l eliminates the need for the user input when GrADS asks about Landscape mode, and the -x tells it to quit when finished. You can add the 'c' followed by the name of a command or script. For example:

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

Will start grads in landscape mode, execute the command that prints out the configuration information, and then quit. The command 'q config' could be replaced by the name of a script. Documentation about command line options for GrADS is at http://iges.org/grads/gadoc/gradcomdgrads.html

JMA
  • 26
  • 3