dongqu9972 2012-07-31 09:41
浏览 62

PHP,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");

echoing x outputs :

your name is

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.

note : i tried doing

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

echo $x;

output was :

Your name is :

i was expecting like

Your name is : Foo

clearly, something is wrong.

  • 写回答

1条回答 默认 最新

  • duangan6133 2012-07-31 10:32
    关注

    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 : 
    

    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.

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?