Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

catch all output with backtics

Status
Not open for further replies.

esromneb

Programmer
Mar 30, 2002
76
US
Hi, I'm writing a script to edit the main.cf config file and I'm having this problem. (I'm on RedHat 9) At the end of the script I want to see if my changes work, and if postfix is happy. To do this, I would think that the following would be just fine:

$postfix_check = `postfix check`;
print ($postfix_check);
if($postfix_check =~ /fatal/) {
print "\nPostfix is broken\n";
}else{
print "\nPostfix file A OK\n";
}

The problem is: the output that postfix gives back goes straight to my screen not to the $postfix_check variable. How can I make the backtick catch all output?
 
Try

$check = 0;
@postfix_check = `postfix check`;

foreach (@postfix_check)
{
if (/fatal/i)
{
print "\nPostfix is broken\n";
$check = 1;
last;
}
}

print "\nPostfix file A OK\n" if ($check != 1);


Hope this helps
 
Sounds like it's printing to STDERR. Try this:
Code:
$postfix_check = `postfix check 2>&1`;
 
I think you may be right about printing to STDERR, but that doesn't help. When I run the perl script I get no output but it still isn't going into my var. It sorta just dissapears ^_^. Thanks for your help.
-ben
 
postfix is located in:
/usr/sbin/postfix

why?
-ben
 
@postfix_check = `/usr/sbin/postfix check`;
might sort it
--Paul
 
It appears the program is running, since you were getting screen output before redirecting STDERR. Does it generate any output if it succeeds, or only if it fails? Is it possible the program is succeeding, and thus not generating any output?

An alternate strategy to consider, if you can't get the backticks method sorted: does the program return a 0 for success, and a non-zero code for failure? If so, you could try executing it with system, and storing the exit code in your $postfix_check variable, e.g.
Code:
$postfix_check = system(qq(postfix check 2>&1));
if($postfix_check) {
    print "\nPostfix is broken\n";
}else{
    print "\nPostfix file A OK\n";
}

 
Actually, if you go with system you won't want any output, so it should be
Code:
$postfix_check = system(qq(postfix check >/dev/null));
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top