Solved

Capturing STDERR Output

Posted on 1999-01-21
2
691 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

830 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