Solved

Capturing STDERR Output

Posted on 1999-01-21
2
687 Views
Last Modified: 2008-03-10
I have a process that return text using STDERR.
I need to run something like:
@out=`process command`;
To get the output, but this gets only STDOUT.
How can I get the STDERR like that in an Easy way?
0
Comment
Question by:eofek
2 Comments
 
LVL 5

Expert Comment

by:thoellri
ID: 1210227
From "perldoc perlfaq8" :

     With any of these, you can change file descriptors before
     the call:

         open(STDOUT, ">logfile");
         system("ls");

     or you can use Bourne shell file-descriptor redirection:

         $output = `$cmd 2>some_file`;
         open (PIPE, "cmd 2>some_file |");

     You can also use file-descriptor redirection to make STDERR
     a duplicate of STDOUT:

         $output = `$cmd 2>&1`;
         open (PIPE, "cmd 2>&1 |");

Hope this helps
   Tobias

0
 

Accepted Solution

by:
meyoz earned 50 total points
ID: 1210228
Use this (I've checked it myself - it's working):

$pipename="pipename";
system("mkfifo -m 600 $pipename"); || die("Cannot make fifo: $!\n");
if (!fork()) {
  close(STDOUT);
  open(STDOUT,">$pipename");
  close(STDERR);
  open(STDERR,">$pipename");
  system("cmd"); # place your command here
  exit(0);
} else {
  open(CATCH,"<$pipename");
# now you have STD(OUT,ERR) of cmd in CATCH
# or you can do so:
#  @out=`cat $pipename`
}
unlink("$pipename");

You will get mixed STDOUT and STDERR
I think it's clear how to capture STDOUT and STDERR separately :)

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question