in reply to capture stdout and stderr from external command
#!/usr/bin/perl use warnings; use strict; use IPC::Open3; use IO::Select; my $pid = open3(\*WRITE, \*READ,\*ERROR,"/bin/bash"); my $sel = new IO::Select(); $sel->add(\*READ); $sel->add(\*ERROR); my($error,$answer)=('',''); while(1){ print "Enter command\n"; chomp(my $query = <STDIN>); #send query to bash print WRITE "$query\n"; foreach my $h ($sel->can_read) { my $buf = ''; if ($h eq \*ERROR) { sysread(ERROR,$buf,4096); if($buf){print "ERROR-> $buf\n"} } else { sysread(READ,$buf,4096); if($buf){print "$query = $buf\n"} } } } waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: capture stdout and stderr from external command
by pankajadvani (Initiate) on Nov 11, 2011 at 19:31 UTC | |
by zwon (Abbot) on Nov 12, 2011 at 03:33 UTC | |
by zentara (Cardinal) on Nov 12, 2011 at 11:15 UTC | |
by remiah (Hermit) on Nov 12, 2011 at 02:22 UTC |