2014-12-31 94 views
0

我正在尋找一種允許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代碼進行修復才能解決這個問題?任何幫助將不勝感激...我的命令在命令行工作正常,所以這是唯一的東西拿回我的應用程序。

+0

可能的重複:http://stackoverflow.com/q/5794030 –

+0

不是真的,該用戶想創建一個文字'PHP外殼',而我正在尋找只需從PHP腳本中使用真正的unix shell。 –

+0

您可以嘗試使用PHP的expect模塊http://php.net/manual/en/expect.examples-usage.php – piotrekkr

回答

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