[英]Why does PHP in “interactive shell” mode only run this script once?
[英]Run an interactive shell command from PHP in CLI mode
我有一个 PHP CLI 脚本,想从该脚本中执行交互式 bash 命令(例如, less huge-file.txt
)并获得相同的视图(例如导航控件less
),就像我直接从终端启动它一样。
普通的system()
调用是不够的,因为它们不会暂停,而是立即返回所有输出(例如,在less
的情况下)。
感觉是我有一个 CLI 脚本来组织几个任务。 其中一些使用复杂的 bash 命令,我只想从 PHP 调用 bash 脚本但获得原始终端行为。
proc_open( 'less huge-file.txt', array( STDIN, STDOUT, STDERR), $pipes);
这将调用命令并传递所有控件等,因此与普通的less huge-file.txt
没有区别。
仍然有点笨重,但与我能找到的其他示例相比要短得多。
对的,这是可能的。 我最近发布了一个项目,允许 PHP 获取真正的 Bash shell 并与之交互。 在这里获取: https : //github.com/merlinthemagic/MTS
我怀疑您是否真的希望使用 less 编写脚本(这会变得丑陋,head / tail / sed /awk 是您在文本操作中的朋友),但是很有可能获得真正的 shell 行为。
下载后,您只需使用以下代码:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', false);
//open file in less. $return1 will contain the view and the first part of the file you opened. The second argument is needed to delimit the return since less will not end in a shell prompt.
$return1 = $shell->exeCmd('less /path/to/huge-file.txt', 'huge-file.txt');
//quit less, you must do this or the shell will hang on the less prompt and will have to be forcefully terminated.
$return2 = $shell->exeCmd('q');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.