本文介绍了PHP,shell_exec和输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

shell_exec是否有可能执行给定的命令,其中初始命令将要求动态输入,然后再执行基于输入本身的命令.

is it possible for shell_exec to execute a given command, where the initial command would ask the for a dynamic input then followed by a command that's based on the input itself.

我已经研究了数小时以寻找答案,但我似乎找不到我想要的东西.

i've researched for hours for the answer and i can't seem to find what i'm looking for.

我有一个与以下示例类似的要求,自

i have a requirement that's similar to the idea of the example below and any help would be appreciated since

$x = shell_exec("read -p 'Enter your name : ' x; echo 'Your name is' : $x");

回显x个输出:

您的名字是

如您所见,我正在运行多个命令,但是我不知道我可以在字符串命令中插入哪里作为输入.

as you can see i'm running multiple commands, but i do not know where i can insert inside the string command for the input.

注意:我尝试做

$x = shell_exec("echo 'Foo' | read -p 'Enter your name : ' x; echo 'Your name is :' $x");

echo $x;

输出为:

您的名字是:

我期望像

您的名字是:Foo

很明显,出了点问题.

推荐答案

其他时间,我遇到了与您相同的问题.如果在终端上执行同一行,则结果是相同的,因此这不是php问题,而是shell问题:

I've come across the same problem as you with read other times. If you execute the same line on the terminal, the result is the same, so this is not a php issue, but a shell issue:

$ echo 'Foo' | read -p 'Enter your name : ' x; echo "Your name is : $x"
Your name is :

如果将read包裹在while .. do .. done内,那么它们都可以完美运行:

If you wrap the read inside a while .. do .. done, then it all works perfectly:

$ echo 'Foo' | while read -p 'Enter your name : ' x; do echo "Your name is : $x" ; done
Your name is : Foo

我不知道为什么会这样.

I don't know why this happens though.

此外,您可以尝试使用 proc_open 和类似的方法,您将获得对输入/输出流,但是我不知道它们是否可以解决read问题.

Also you can try using proc_open and similar, and you'll get more control of the input/output streams, but I don't know whether they will work with the read issue.

这篇关于PHP,shell_exec和输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 01:43