LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   math programming help in shell (https://www.linuxquestions.org/questions/linux-newbie-8/math-programming-help-in-shell-4175432329/)

sniper8752 10-15-2012 12:33 PM

math programming help in shell
 
i am trying to do some math for a program, and i am getting an error. here is my code:
let perimeter=2*$l+2*$w

error:
rectangle: line 6: let: perimeter=2*: syntax error: operand expected (error token is "*")

Habitual 10-15-2012 12:41 PM

show us the entire script please.
[code][/code] tags Please.
and bookmark these...
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.gnu.org/software/bash/man...ode/index.html
http://www.grymoire.com/Unix/Sh.html
http://tldp.org/LDP/abs/abs-guide.pdf
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls

suicidaleggroll 10-15-2012 12:41 PM

The shell is not very good at doing math directly, but you can use other programs for this, such as bc.

Code:

perimeter=$(echo "2*$l+2*$w" | bc -l)

JaseP 10-15-2012 01:07 PM

Regular expressions ...

See;
http://www.freeos.com/guides/lsst/ch02sec07.html

You have to use escape characters to preserve mathematical functions...


All times are GMT -5. The time now is 05:10 PM.