Home > Archive > PERL Beginners > January 2006 > Problem with STDOUT redirect
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problem with STDOUT redirect
|
|
| John Maverick 2006-01-23, 3:55 am |
| I am trying to write a wrapper to invoke a script which takes input from
STDIN.
I am opening a filehandle so that I can store all information returned by
invoked script.
However, it hangs when second script is waiting for user input. Input
varies from time to time.
So, I can't use open(SCRIPT, "| @cmd ") and send values to SCRIPT. Is there
any way I can make sure second program gets input from user and store all
information in array or somewhere I can use for sending in email?
Thanks for your help in advance.
sub executeScript {
my (@cmd) = @_;
my $status = 0;
my $comment = "\nRunning " . join(" ", @cmd) . " ...\n";
push(@msg, $comment);
my $pid = open(SCRIPT, " @cmd |") or die "Couldn't run @cmd: $!\n";
while (<SCRIPT> ) {
#add STDOUT or STDERR from script being run to @msg for email output
chomp;
push (@msg, $_);
}
unless(close(SCRIPT)) {
$status = 1;
push(@msg, "@cmd failed\n");
}
return $status;
}
| |
| Zentara 2006-01-23, 6:56 pm |
| On Sun, 22 Jan 2006 23:58:09 -0800, john.maverick1975@gmail.com (John
Maverick) wrote:
>I am trying to write a wrapper to invoke a script which takes input from
>STDIN.
>I am opening a filehandle so that I can store all information returned by
>invoked script.
>However, it hangs when second script is waiting for user input. Input
>varies from time to time.
>So, I can't use open(SCRIPT, "| @cmd ") and send values to SCRIPT. Is there
>any way I can make sure second program gets input from user and store all
>information in array or somewhere I can use for sending in email?
>
>Thanks for your help in advance.
>
I find it hard to understand exactly what you are asking, but I think
you are looking for IPC::Open3 This is a super simple example of usage.
Usually you would have it in a loop, and/or use IO::Select on the READ
and ERROR filehandles. There are tons of examples for IPC::Open3
on groups.google.com
use IPC::Open3;
my $pid = open3(\*WRITE, \*READ,\*ERROR,"./myscript myarg");
#if \*ERROR is false, STDERR is sent to STDOUT
#send stuff to script
print "Please enter some info\n";
my $someinfo = <>;
print WRITE "$some info";
#also store $someinfo in another file if you desire
#get output
while( my $output = <READ> ) {
#can send this to files
print "output->$output";
}
while( my $errout = <ERROR> ) {
#can send this to files
print "err->$errout";
}
waitpid( $pid, 0 ) or die "$!\n";
my $retval = $?;
print "pid->$pid\nretval->$retval\n";
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
|
|
|
|
|