Perl backticks when using pipes
Domanda
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?
Soluzione
$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.
Altri suggerimenti
The backticks are deprecated. Use the qx(..)
syntax instead.
$timeTotal = qx(echo $timeTotal + $time | bc -l");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow