2014-12-31 6 views
0

PHP 스크립트가 프롬프트되었을 때 여러 명령을 보낼 수있는 솔루션을 찾고 있습니다. 다음 코드는 쉘에서 실행되는 경우 :PHP에서 쉘 스크립트에 명령 줄 입력 대기

[email protected] [~]# /usr/local/bin/grads-2.0.2/bin/grads -b 

이 출력 결과 : 당신이 볼 수 있듯이, 스크립트가 Y/N 입력을 기다리고

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이 입력되면, 다음과 같은 출력 결과 : 그것이 '종료'명령으로 종료 될 때까지

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 스크립트에서 실제 유닉스 셸을 사용하십시오. –

+0

PHP의 expect 모듈을 사용해 볼 수도 있습니다. http://php.net/manual/en/expect.examples-usage.php – piotrekkr

답변

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