Solved

use SYSTEM

Posted on 2003-10-29
4
162 Views
Last Modified: 2010-03-04
Hello,

What i am trying to do is to use "system" to invoke a program and catch the execution result. However, any output and error message produced by the program during execution are redirected to a file. The following script does not work because the last two fields are also passed to the program. SO how can fix this? Thanks.

$result = system "program args", ">temFile", "2>&1";

if ($result) {
 print "error\n";
 }else {
 print "ok\n";
}

0
Comment
Question by:rongz
[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
4 Comments
 
LVL 2

Expert Comment

by:ultimatemike
ID: 9641529
You don't necessarily need to use system. If all you're concerned about is the output of the program, you can try:



#!perl -w


use strict;


open FH, "$program_name |";

while (<FH>) {
      print;
}



The pipe in the open command will cause Perl to read the standard output of the program being run.
0
 

Author Comment

by:rongz
ID: 9642263
Actually, what I want is that user can just see if the program is executed sucessfully or not, and output (including error message) from the program is redirected to a file.
0
 
LVL 2

Accepted Solution

by:
ultimatemike earned 50 total points
ID: 9642299
Make the command string that is being executed contain the redirects, rather than as parameters. That seems to work.


Try this:


$result = system "program args >temFile 2>&1";

if ($result) {
 print "error\n";
 }else {
 print "ok\n";
}
0
 
LVL 2

Expert Comment

by:ChrisDrake
ID: 9648061
You might need the perl variable:-

   $?

Which contains the exit code of the program you ran.

$rc=`program args >&temFile`;
print "It returned $?\n";
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
perl script to count sepecial characters in a file 7 149
Perl script to parse xml file 2 144
compress files in RAR using perl 13 94
Convert grep lines to perl 6 101
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…
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 …
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…

752 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