A-Kay
A-Kay

Reputation: 69

Perl script interacting with another program's STDIN

I have a Perl script that calls another program with backticks, and checks the output for certain strings. This is running fine.

The problem I have is when the other program fails on what it is doing and waits for user input. It requires the user to press enter twice before quitting the program.

How do I tell my Perl script to press enter twice on this program?

Upvotes: 1

Views: 997

Answers (2)

Sebastian
Sebastian

Reputation: 2550

The started command receives the same STDIN and STDERR from your script, just STDOUT is piped to your script.

You could just close your STDIN before running the command and there will be no input source. Reading from STDIN will cause an error and the called command will exit:

close STDIN;
my @slines = `$command`;

This will also void any chance of console input to your script.

Another approach would use IPC::Open2 which allows your script to control STDIN and STDOUT of the command at the same time:

use IPC::Open2;
open2($chld_in, $chld_in, 'some cmd and args');
print $chld_in "\n\n";
close $chld_in;
@slines = <$chld_out>;
close $chld_out;

This script provides the two \n input needed by the command and reads the command output.

Upvotes: 3

Dan Walmsley
Dan Walmsley

Reputation: 2821

You could just pipe them in:

echo "\n\n" | yourcommand

Upvotes: 2

Related Questions