LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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: 419

Rep: Reputation: 31
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: 419

Original Poster
Rep: Reputation: 31
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: 65
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
 
Old 07-21-2018, 03:00 PM   #5
frappyjohn
LQ Newbie
 
Registered: Jan 2007
Posts: 1

Rep: Reputation: 1
Using Remainder (modulo) operator with integer division (e.g. converting total seconds to days, hours, minutes, secs)

The modulo (%) operator is often useful when doing integer division. This routine converts total lapsed seconds to a more human comprehensible format:

Code:
#!/bin/bash
# Convert Arg 1 (total seconds) to human readable string of weeks, days, hours, minutes, and seconds
TOTALSECONDS=$1

MINUTES=$((TOTALSECONDS / 60))
SECONDSREMAINING=$((TOTALSECONDS % 60))

HOURS=$((MINUTES / 60))
MINUTESREMAINING=$((MINUTES % 60))

DAYS=$((HOURS / 24))
HOURSREMAINING=$((HOURS % 24))

WEEKS=$((DAYS / 7))
DAYSREMAINING=$((DAYS % 7))

echo $TOTALSECONDS secs = $WEEKS wks $DAYSREMAINING days $HOURSREMAINING hrs $MINUTESREMAINING mins $SECONDSREMAINING
 
1 members found this post helpful.
Old 07-21-2018, 10:48 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by babag View Post
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
I think you got'er figured out but yeah this typ O'
Code:
INCREMENTS=($FRAMES / $PROCESSORS)
should be
INCREMENTS=$(($FRAMES / $PROCESSORS))
example, CLI
Code:
userx@manjaro:~
$ g=8
 
$ d=2
 
$ v=$(($g/$d))
 
$ echo $v
4
 
Old 07-31-2018, 06:10 PM   #7
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
By the way, you don’t need ‘$’ inside of ‘$((…))’. Some even claim that it’s less error prone. Compare:
Code:
]$ a=2+2
$ echo $((a * 2))
8
$ echo $(($a * 2))
6
 
1 members found this post helpful.
Old 08-01-2018, 06:31 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by mina86 View Post
By the way, you don’t need ‘$’ inside of ‘$((…))’. Some even claim that it’s less error prone. Compare:
Code:
]$ a=2+2
$ echo $((a * 2))
8
$ echo $(($a * 2))
6
i wouldn't say less error prone - it seems both use cases are treated differently:
the first, counts 2+2=4 BEFORE multiplying it, the second uses the literal string 2+2.
while the second is mathematically correct, you cannot say that either of them is wrong because of the unique way bash (and all shells i believe) can treat strings as numbers and vice versa.

i would never use a construct like 'a=2+2' inside a bash script. if anything, that's where the error is, it's confusing mathematical operations with variable assigment.

PS: i'm perfectly aware this is a necrobumped thread, but why not...
 
2 members found this post helpful.
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 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
Open Source Consulting | Domain Registration