1

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?

1

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I found that using close STDIN didn't allow me to capture the full output of the program that's running. I'm assuming there's a typo in your code - it should be open2($chld_out, $chld_in, 'some cmd and args'); ? However I'm still having the same problem - I read that it would be because I'm running on Windows?
I managed to get this working by adding two lines after the open2 line: sleep(10); print $chld_in;- i'm assuming that it was sending to the child before it was ready to accept the \n\n and I found elsewhere that print $chld_in helps on Windows systems. Thanks @Sebastian for pointing me in the right direction
2

You could just pipe them in:

echo "\n\n" | yourcommand

1 Comment

My current code looks like this: my @slines = "$command; Would it then become: my @slines = echo "\n\n" | "$command; ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.