Solved

Bash Calulation

Posted on 2011-09-07
5
287 Views
Last Modified: 2012-05-12
HI,

I have a bash script and need to do a calculation and return an integer.
The issue I have is expr and bc don't deal with decimal exponents.

This is the excel equation I am trying to get working in bash.
ANSWER=INT($START*$CPU^0.422)

Does anyone know how to do this in bash and just return the rounded down value?
All decimal answers should be rounded down.

Thanks
Fixitben
0
Comment
Question by:fixitben
5 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36498629
echo $START $CPU | awk '{printf "%d", $1*$2^0.422}'

or, to use the variables inside awk and to avoid "echo"

1)
awk 'BEGIN {printf "%d", '$START'*'$CPU'^0.422}'

2)

awk -v S=$START -v C=$CPU 'BEGIN {printf "%d", S*C^0.42}'

wmp
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 36498637
The full statement

ANSWER=$(awk -v S=$START -v C=$CPU 'BEGIN {printf "%d", S*C^0.42}')
0
 
LVL 7

Author Closing Comment

by:fixitben
ID: 36498689
That worked great. Thanks
0
 
LVL 20

Expert Comment

by:edster9999
ID: 36498747
bc is a command that does just that.  If you do not specify the 'scale' to give it the number of decimal point places to use then it assumes no places and rounds down to the nearest integer.

Now the next step.  You can't do 'to the power of' in bc with a real number.  The value to the right has to be an integer.  Its just not going to handle that.

You'll need to convert it first.  Get your maths text book out and look up :
x^y = e^(y*log(x))
This is an alternative way of working out x^y and can  be handled by bc.
I've never done this part so you'll have to play round with it.
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 36498948
With bc your answer would have been:

START=1.5
CPU=2
echo "e(0.477*l(${START}*${CPU}))" | bc -l
1.68883351312073475842

Open in new window


and rounded:

START=1.5
CPU=2
echo "e(0.477*l(${START}*${CPU}))" | bc -l | xargs printf "%1.0f\n"
2

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now