0

プロンプトが表示されたときに PHP スクリプトが複数のコマンドを送信できるようにするソリューションを探しています。次のコードがシェルから実行されると:

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 についてはどうすることもできませんが、"-b" の代わりに "-blx" を指定して grads を呼び出すと、自動的にランドスケープ モードで起動し、コマンドが終了すると終了します。-l を指定すると、GrADS がランドスケープ モードについて尋ねるときにユーザー入力が不要になり、 -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 に答える