Re: automatically giving user input to a command line
by zentara (Cardinal) on Jun 13, 2012 at 20:02 UTC
|
Are you trying to automate this script? If so, you could use Expect.
If you are looking for a timeout, try something like this:
#!/usr/bin/perl
use warnings;
use strict;
# may not work on windows
use Term::ReadLine;
my $t = new Term::ReadLine 'timeout test';
my $input;
eval {
local $SIG{ALRM} = sub { die ("timeout\n") };
alarm 10;
$input = $t->readline("Enter something within 10 seconds: ");
alarm 0;
};
print(
( $@ =~ /timeout/ )
? "\nYou loose!\n"
: "You win: $input\n"
);
| [reply] [d/l] |
|
|
I should clarify, I am not looking for a timeout, as the program halts until an actual user enters something into the command window during execution. I am looking for the script to issue what a physical person needs to do when it prompts with the screen
"Do you want to read the file? 1 for yes, 2 for no. (Default is 2)"
in this case, if someone was physically at the computer during execution and they wanted to read the file, they would type "1" and hit enter. That is what I am looking to have my script do automatically. I looked into expect, and their are two primary issues with it unfortunately.
One is it appears to depend on using files and I am not using any files, perhaps I am incorrect on this?
Two, I am developing this for a company where I can't put this on our systems without waiting past a time where the deadline for the larger piece of code that this script is associated with.
Edit: I have decided to retrieve Expect for my local system at least and begin the process of making point two of this post moot. Hopefully Expect will be able accomplish what I need.
| [reply] [d/l] |
|
|
I'm not sure why you can't use Expect, but the other alternative is to use IPC, read perldoc perlipcHere is a super simple example using the bc calculator, but you would run your script. This uses IPC::Open2 which comes standard in Perl. Just plugin your script, and answer the prompts in order as you receive them.
#!/usr/bin/perl
#prompts for an string to evalute
#(line 2+2, or 5x7, 5*6 / 3 , etc)
#sends it to the bc calculator,
#then reads the answer, and prints.
use IPC::Open2;
use strict;
use warnings;
my ($rd, $wr);
open2($rd, $wr, "bc");
print "Enter a string to evaluate\n";
my $prompt= <STDIN>;
print $wr "$prompt";
my $x = <$rd>;
print $x;
print "Enter another string to evaluate\n";
my $prompt= <STDIN>;
print $wr "$prompt";
my $x = <$rd>;
print $x;
close($rd);
close($wr);
| [reply] [d/l] |
|
|
Re: automatically giving user input to a command line
by BrowserUk (Patriarch) on Jun 13, 2012 at 20:32 UTC
|
$output = `echo 1 | command foo`;
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
| [reply] [d/l] |
|
|
I did try that, and unfortunately I am given the same problem as detailed in a previous comment, where it returns control to the user, and waits until the user does something, whether that be hitting 1 and then enter or something of the like. Thank you for the help though, I appreciate it.
| [reply] |
|
|
it returns control to the user, and waits until the user does something, whether that be hitting 1 and then enter or something of the like.
That suggests that the program is not accepting input via STDIN but rather via some form of direct access to the keyboard buffer, in which case, pretty much all of the mechanisms whereby you might try to control it from Perl will fail.
What OS are you doing this on?
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
| [reply] |
|
|
|
|
|
|
|
Re: automatically giving user input to a command line
by morgon (Priest) on Jun 13, 2012 at 21:27 UTC
|
What you have tried cannot work.
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:
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");
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).
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). | [reply] [d/l] |
|
|
I am currently in the process of obtaining Expect, however I ran into some errors installing it properly, and from what I have researched, I can not do it properly without cygwin. So I am in the process of installing it on my work system (Why I didn't have it before I don't know. I normally do) and as soon as I get it, I should be able to install it and start testing with it.
| [reply] |