LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-15-2012, 01:33 PM   #1
sniper8752
Member
 
Registered: Oct 2012
Posts: 339

Rep: Reputation: Disabled
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 "*")
 
Old 10-15-2012, 01:41 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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)
 
1 members found this post helpful.
Old 10-15-2012, 02:07 PM   #4
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 12.04/14.04, Scientific Linux 6.3/6.4, Android-x86, Pretty much all distros at one point...
Posts: 1,791

Rep: Reputation: 157Reputation: 157
Regular expressions ...

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

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking up math functions for C programming on Linux AJones Linux - Software 7 12-15-2009 11:19 PM
How important is math in CS and programming? aguire Programming 12 04-24-2007 12:15 AM
Database programming like math? halfpower Programming 4 09-14-2006 08:27 PM


All times are GMT -5. The time now is 06:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration