Solved

Capturing STDERR Output

Posted on 1999-01-21
2
693 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
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…
Six Sigma Control Plans

695 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