LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-03-2008, 02:12 PM   #1
babag
Member
 
Registered: Aug 2003
Posts: 365

Rep: Reputation: 30
bash simple math (division) question


what's wrong with my scripting here?

Code:
PROCESSORS=6
FRAMES=23715
INCREMENTS=($FRAMES / $PROCESSORS)

echo $INCREMENTS
i'm expecting a return of 3952 for $INCREMENTS.
instead i keep getting either a blank space or
something like the text $FRAMES.

thanks,
BabaG
 
Old 04-03-2008, 02:24 PM   #2
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
[me@linuxbox me]$ echo $((2+2))
4

As you can see, when you surround an arithmetic expression with the double parentheses, the shell will perform arithmetic evaluation.

Try , something like...
hours=$((seconds / 3600))

Gentoo

Last edited by prad77; 04-17-2008 at 03:40 AM.
 
Old 04-03-2008, 02:37 PM   #3
babag
Member
 
Registered: Aug 2003
Posts: 365

Original Poster
Rep: Reputation: 30
thanks prad77!
 
Old 04-03-2008, 05:24 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Note that bash only does integer mat internally. If you want to do floating point arithmatic, you will need to used some sort of external program. Commonly used is bc, for example:
Code:
$ cat t.sh
#!/bin/bash

result=$(echo "7 / 3" |bc -l)
echo "result = $result"

$ ./t.sh
result = 2.33333333333333333333
 
  


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
simple bash question. kalleanka Programming 2 11-16-2006 07:45 PM
Syntax for performing bash simple math calculations kinetik Linux - General 3 03-31-2006 08:03 PM
math division in javascript rblampain Programming 2 09-07-2005 04:07 AM
stuck again (noob bash math question) babag Programming 6 04-25-2005 01:27 AM
bash and math division problem bennethos Programming 5 10-17-2004 01:51 PM


All times are GMT -5. The time now is 11:23 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration