awohld has asked for the wisdom of the Perl Monks concerning the following question:

I'm running a script in Unix, after every line that print's I need to simulate <ENTER>, I read in unix this is \n.

So for example I type "perl script.pl" which executes this script. So the first command that the script runs is "execopu -text". Then I get a prompt for more data and then I need to script to feed "cell" to the program, and so on for the rest of the "print's" that I do to the screen.

Right now the script just prints all the text to the screen and nothing executes.
#!/usr/bin/perl my @sitenumbers = (7,12); print "execopu -text\n"; print "cell\n"; foreach my $site_number (@sitenumbers){ print <<EOF; u csno = $site_number ccuoosmin_3g1x = "" ccuoos_3g1x = "" u EOF }
The screen should read like this
>perl script.pl >execopu -text #This comes from my script Enter form name: cell #The computer asks for the form name +and the script populates it with "cell" and then <ENTER> Enter Operation: u #The computer asks for "Operation" an +d the script populates it with "u" and and then <Enter> Enter Value: csno = $site_number
And this should continue for the rest of the script.

Replies are listed 'Best First'.
Re: Auto Feeding Data to Unix Programs
by wazoox (Prior) on Apr 19, 2005 at 22:36 UTC

    You need two different things : first grab output from the external program, secondly feed it with your response. That's exactly what IPC::Open2 is for.

    Search this site for examples using Open2, they abound.

    If you want a more complex interaction, perhaps should you look at Expect instead... YMMV.

Re: Auto Feeding Data to Unix Programs
by fauria (Deacon) on Apr 19, 2005 at 22:53 UTC
    If you want to fill values automaticallly as they prompt, you can use Expect module for Perl.
Re: Auto Feeding Data to Unix Programs
by ikegami (Patriarch) on Apr 19, 2005 at 22:32 UTC

    Are you trying to send text to the execopu program? If so, check out Perl's open(FH, '|...') function:

    #!/usr/bin/perl open(EXECOPU, '| execopu -text') or die("Can't run execopu: $!\n"); my @site_numbers = (7, 12); print EXECOPU "cell\n"; foreach my $site_number (@site_numbers) { print EXECOPU <<"EOF"; u csno = $site_number ccuoosmin_3g1x = "" ccuoos_3g1x = "" u EOF }

    You might also want to check our IPC::Open2, IPC::Open3 and IPC::Run, especially if you want to capture the output.

      Yes, I am trying to send all that text to the "execopu" program. It keeps on prompting me for input and I need to feed it automatically.
      When I feed "u" to the script I get this error:

      FD00 - Cannot convert ODIN stringwad to subschema. Check for a null field and/or verify the RC/V product and mask are compatible.

      Any idea if a null field is getting fed?
        What's a null field? I'm not familiar with execopu, so you'll have to give us more info. I think you might need to remove the "u" at the bottom of the heredoc (i.e. I think you might need to delete the second line with the "u", right before the EOF).