0

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?

jarandaf
  • 4,297
  • 6
  • 38
  • 67
  • 8
    You're aware that Perl can do addition, right? – friedo Sep 21 '12 at 19:21
  • 3
    Slightly off topic: Why on earth are you using a shell command to do (very) simple math? `$timetotal += $time` is the perl code you are looking for. – TLP Sep 21 '12 at 19:27

2 Answers2

6

$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.

ikegami
  • 367,544
  • 15
  • 269
  • 518
-4

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

$timeTotal = qx(echo $timeTotal + $time | bc -l");
David W.
  • 105,218
  • 39
  • 216
  • 337
  • 2
    Backticks are deprecated? Do you have a source for that? – TLP Sep 21 '12 at 20:29
  • 2
    No mentioned of them being deprecated in the [latest documentation](http://perl5.git.perl.org/perl.git/blob/56e2d9fbd5fb964af1b71d8a8546c889702991ea:/pod/perlop.pod#l2136). And this doesn't answer the question at all. – ikegami Sep 21 '12 at 20:41