Daniel Daniel - 2 months ago 19
Perl Question

Perl, execute bash command with pipe

Sorry if my English is bad.

I have a bash command:

convert somefile1.png somefile2.png -resize '200x200!' MIFF:- | compare -metric AE -fuzz '5%' - null:-


That command returns number, how can I get this value in my perl script?

My code:

my $s_command =
"convert " . $s_img1_path . " " .
$s_img2_path . " -resize '" .
$N_IMAGEMAGICK_IMG_SIZE . "x" . $N_IMAGEMAGICK_IMG_SIZE .
"!' MIFF:- | compare -metric AE -fuzz '5%' - null:-";

my $s_out = `$s_command`;


But I can't get the return value in $s_out. Moreover, this value goes to stdout itself, why?

Answer

I ran the following code:

#!/usr/bin/perl -w
use strict;

my $cmd = "grep strict test.pl -n|cut -c1-12";
my $result = `$cmd`;
print $result;

The output to STDOUT was as expected (I ran the grep on this code, actually):

2:use strict
4:my $cmd = 

When I commented out the print line, there was no output to STDOUT. Maybe the problem has something to do with STDERR, you should check that.