in reply to automatically giving user input to a command line
When you use backticks you get control back only AFTER the command has finished, which of course is too late for your purpose.
Expect should be what you need, something like:
This spawns your command in parallel to your script and waits for your prompt to appear. It then sends a "1" back to the command, simulating a user input (depending on how your command reads it's input you may or may not have to send the newline - you have to experiment there).use strict; use Expect; my $ex = Expect->spawn(undef, "Command foo") or die $! $ex->expect(undef, "-re", "Do you want to read the file"); $ex->send("1\n");
If you want you can add a timeout as the first parameter to the expect-call (undef means wait forever for the pattern to appear which may or may not be what you want).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: automatically giving user input to a command line
by tfredett (Sexton) on Jun 14, 2012 at 14:47 UTC |