简体   繁体   English

使用PHP填充Sh脚本值

[英]Fill Sh script values using PHP

I have a ready sh script to do some tasks I want, and this sh script shows a menu after execution for example : 我有一个现成的sh脚本可以执行我想要的一些任务,该sh脚本在执行后显示一个菜单,例如:

Enter 1 to Select Menu 1 or 2 to select Menu 2

 1. Menu 1
 2. Menu 2

Then : 然后 :

Enter a number to install X addon :

 1. Addon 1
 2. Addon 2

Currently, I use in my PHP script to execute the Sh script : 当前,我在PHP脚本中使用以执行Sh脚本:

echo shell_exec('sh /etc/share/nginx/autoload-rev5.sh');

How can I fill the values required by the Sh script from PHP without modifying the Sh script? 如何在不修改Sh脚本的情况下从PHP填充Sh脚本所需的值? is it possible ? 可能吗 ?

You could try using expect - http://php.net/manual/en/expect.examples-usage.php 您可以尝试使用期望-http://php.net/manual/en/expect.examples-usage.php

This is pretty much the same as the expect Linux tool, but you are coding in PHP 这与期望的Linux工具几乎相同,但是您使用PHP进行编码

You can see some more examples here: http://php.net/manual/en/function.expect-expectl.php 您可以在此处看到更多示例: http : //php.net/manual/en/function.expect-expectl.php

Basically pattern match shell output and on match, submit a predefined response 基本上是模式匹配shell输出,并在匹配时提交预定义的响应

You can do this using proc_open function. 您可以使用proc_open函数执行此操作。 Call it to create a handle on your script (instead of using shell_exec) then parse its output and write what you want to its input. 调用它在脚本上创建一个句柄(而不是使用shell_exec),然后解析其输出并将所需内容写入其输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM