Solved

Called Program Status

Posted on 2011-02-24
5
459 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
[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
  • 2
5 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34976755
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
ID: 34976797
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
ID: 34976825
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
ID: 34976860
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
ID: 34976902
clockwatcher gets it right with $?

Learn something every day.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl search and replace 6 172
Perl script to parse xml file 2 132
Exchange 2010 Transport Rule Regex 28 132
Any syntax error for this clone.plscript 6 144
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

749 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