jarandaf
jarandaf

Reputation: 4427

Perl backticks when using pipes

I'm experiencing some problems trying to capture the output of a simple command:

$timeTotal = `echo $timeTotal + $time | bc -l`;

But I'm getting the following errors:

sh: +: not found
sh: Syntax error: "|" unexpected

This command works perfectly in bash but it seems sh is being actually used. At the very beginning I thought that the problem is the pipe usage (although the sum is not well interpreted neither). What confuses me is that the following command in the same script causes no error and works properly:

my $time = `cat $out.$step | bc -l`;

Any suggestions?

Upvotes: 0

Views: 2045

Answers (2)

David W.
David W.

Reputation: 107040

The backticks are deprecated. Use the qx(..) syntax instead.

$timeTotal = qx(echo $timeTotal + $time | bc -l");

Upvotes: -4

ikegami
ikegami

Reputation: 385897

$timeTotal contains a trailing newline it shouldn't, so you're executing

echo XXX

and

+ YYY | bc -l

instead of

echo XXX + YYY | bc -l

You're surely missing a chomp somewhere.

There's also a double-quote in your command that's out of place.

Upvotes: 6

Related Questions