Search code examples
bashbc

Syntax error on bash shell trying to perform a simple calculation


I'm having a syntax error ((standard_in): syntax error)on 3rd and 5th line.

#!/bin/bash
i=`echo "8.8007751822"|bc`
rws = `echo "0.49237251092*$i" |bc`
rmt = `echo "0.85 * $rws"| bc`
dx  = `echo "log ($rws / 0.000001) / 720.0" | bc`;

Can anyone help me?


Solution

  • A few things:

    • Assignments must not have blanks around the =
    • i=`echo "8.8007751822"|bc` is a really complicated way to write i=8.8007751822
    • bc has no function log, there's only l for the natural logarithm (and l requires the -l option to be enabled)

    I would move everything into bc instead of calling it multiple times:

    bc -l <<'EOF'
    i = 8.8007751822
    rws = i * 0.49237251092
    rmt = 0.85 * rws
    dx = (l(rws / 0.000001) / l(10)) / 720
    dx
    EOF
    

    This prints the value of dx.