c-a
c-a

Reputation: 6058

PHP CLI - Piping and prompting at the same time?

I have two php cli scripts. The first one pipes data into the second one. I want the second one to prompt the user for a confirmation. Is this possible?

I'm using wordpress' php-cli-tools, but my problem seem to be applicable to php in general, from what I see with my basic tests. (i.e. fgets(), readline, etc )

I can never get the prompt to work, since it looks like it will always read an EOT character from the previous input, even if I've already read it...

Then, later, when I prompt by using fwrite for output and then fgets() for input, nothing happens. Program is just waiting for something to happen... Not sure what... If I use the library I get the error 'Caught ^D during input'. No luck with readline either...

Has anyone ever done this before?

Thank you so much!

Upvotes: 0

Views: 151

Answers (1)

Barmar
Barmar

Reputation: 780974

The problem isn't really specific to PHP, it applies to any language. On Unix, you can read from /dev/tty instead of from standard input if you want to ignore input redirection and read from the terminal directly.

function prompt_user($prompt) {
    $terminal = fopen("/dev/tty", "r+");
    if ($terminal) {
        fputs($terminal, $prompt);
        return fgets($terminal);
    }
}

Upvotes: 1

Related Questions