Solved

Called Program Status

Posted on 2011-02-24
5
426 Views
Last Modified: 2012-05-11
I have a perl script that calls another program in such a way that it records the results of the command in a string. i.e.

$program_results = `program $ARGV[0] `;

I would like to have perl check the status of the command, for instance did it complete with a non-error of zero - or did it error out somehow. Is there a way of checking the status of  a program that is run while capturing the output?
0
Comment
Question by:stakor
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
To get the return status, use the system function instead of back-ticks:

$program_results = system("program $ARGV[0] >nul: 2>nul:");

Open in new window


I added code to prevent other output produced by program from also being returned, but this is up to you.
0
 

Author Comment

by:stakor
Comment Utility
I don't fully understand.  I am hoping to capture everything that program would normally print to screen in $program_results. I would also like to have the ability to run a check on another variable to see the exit status. I didn't know if there was a variable built into perl that would show the exit status of the last command or something along those lines.

If I understand the program snippet above, $program_results is the exit variable?
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
Comment Utility
Try $?.

$program_results = `program $ARGV[0] `;
$program_retval = $? >> 8;

perldoc perlvar:

       $?      The status returned by the last pipe close, backtick ("``")
               command, successful call to wait() or waitpid(), or from the
               system() operator.  This is just the 16-bit status word
               returned by the traditional Unix wait() system call (or else is
               made up to look like it).  Thus, the exit value of the
               subprocess is really ("$? >> 8"), and "$? & 127" gives which
               signal, if any, the process died from, and "$? & 128" reports
               whether there was a core dump.
0
 

Author Comment

by:stakor
Comment Utility
So if everything ran correctly, $program_retval should be equal to zero?

if ($program_retval != 0) {
   <freak_out>
};

0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
clockwatcher gets it right with $?

Learn something every day.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now