views:

52

answers:

4

I have a command line application I need to execute from my PHP web application. Say the command is the following:

foo -arg1 -arg2 -arg3

Based on certain conditions, the command line application will prompt user to hit the enter key (e.g. "Please press enter to continue.").

From my PHP application, how do I execute the command line AND send the enter key as response to the prompt?

I'm developing on WAMP. Production code is LAMP.

A: 

Have you tried echo "\n" > foo -arg1 -arg2 -arg3 ?

Kris
No, I have not. How do I execute such a command from a web application such that it knows I am trying to run a command line? I usually use exec(). You have echo(), which displays output to the browser.
StackOverflowNewbie
no, that would've been `shell_exec('echo "\n" > ...')`, i just omitted the php exec code for brevity. I think it needs to be `shell_exec('foo -arg1... | echo "\n"')` though come to think of it.
Kris
It should be 'echo "\n" | foo ...'
too much php
hmm, tested with echo "q" to less /var/log/some_log and it looked like it worked but ofcoarse, it wrote a file named "less" instead of actually running less. oh well
Kris
+1  A: 

You will really need to open a process handle and parse the programs output and write appropriate output in response.

Check out the expect extension though, which can make this sort of thing easier.

Paul Dixon
+2  A: 

That's what the 'yes' program is for. It dumps an endless stream of 'y\n' (or whatever you tell it to via arguments) to the program. It exists for this purpose (answering 'yes' to "do you want to continue" prompts).

shell_exec('yes | foo -arg1 -arg2 -arg3')
zebediah49
Looks like OP's dev environment is Windows. I think `yes` is *nix only...
grossvogel
A: 
$value = fgets(STDIN);

This will allow the user to enter in a value, which you can then access via $value.

Ben Rowe
From the question, it seems that the php script is not running from the command-line...
grossvogel