รออินพุตบรรทัดคำสั่งไปยังเชลล์สคริปต์จาก PHP

ฉันกำลังมองหาโซลูชันที่ช่วยให้สคริปต์ 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 อะไรบ้างเพื่อแก้ไขปัญหานี้ ความช่วยเหลือใดๆ จะได้รับการชื่นชมอย่างมาก...คำสั่งของฉันทำงานได้ดีที่บรรทัดคำสั่ง ดังนั้นนี่จึงเป็นสิ่งเดียวที่ทำให้แอปพลิเคชันของฉันหยุดทำงาน


person B. Schmidt    schedule 31.12.2014    source แหล่งที่มา
comment
อาจซ้ำกับ: stackoverflow.com/q/5794030   -  person    schedule 31.12.2014
comment
ไม่จริง ผู้ใช้รายนั้นต้องการสร้าง 'เชลล์ PHP' ตามตัวอักษร ในขณะที่ฉันต้องการใช้เชลล์ยูนิกซ์จริงจากสคริปต์ PHP   -  person B. Schmidt    schedule 31.12.2014
comment
คุณสามารถลองใช้โมดูลคาดหวังสำหรับ PHP php.net/manual/en/expect example-usage.php   -  person piotrekkr    schedule 31.12.2014


คำตอบ (1)


ฉันไม่สามารถช่วยเกี่ยวกับ PHP ได้ แต่ถ้าคุณเรียกใช้ grads ด้วย "-blx" แทนที่จะเป็น "-b" มันจะเริ่มต้นในโหมดแนวนอนโดยอัตโนมัติ และออกเมื่อคำสั่งของคุณเสร็จสิ้น -l ขจัดความจำเป็นในการป้อนข้อมูลของผู้ใช้เมื่อ GrADS ถามเกี่ยวกับโหมดแนวนอน และ -x บอกให้ออกเมื่อเสร็จสิ้น คุณสามารถเพิ่ม 'c' ตามด้วยชื่อของคำสั่งหรือสคริปต์ ตัวอย่างเช่น:

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

จะเริ่มเรียนจบในโหมดแนวนอน ดำเนินการคำสั่งที่พิมพ์ข้อมูลการกำหนดค่า จากนั้นจึงออก คำสั่ง 'q config' สามารถแทนที่ด้วยชื่อของสคริปต์ได้ เอกสารเกี่ยวกับตัวเลือกบรรทัดคำสั่งสำหรับ GrADS อยู่ที่ http://iges.org/grads/gadoc/gradcomdgrads.html

person JMA    schedule 12.11.2015