in reply to automatically giving user input to a command line

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" );

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: automatically giving user input to a command line
by tfredett (Sexton) on Jun 13, 2012 at 20:15 UTC

    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.

      I'm not sure why you can't use Expect, but the other alternative is to use IPC, read perldoc perlipc

      Here 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);

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh

        this seems like a feasible solution, thanks for the assistance!