Mathematical calculations in shellscript


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Mathematical calculations in shellscript
# 1  
Old 01-05-2012
Mathematical calculations in shellscript

Hi

want to do below mathematical calculations in shellscrip but it is not giving me the exact output.Please help me to solve this

Code:
price=95.3
price1=(20/100)*$price+$price
echo "price=$price1"


finally the output should display price=114.36

Thanks
# 2  
Old 01-05-2012
Shell itself doesn't do floating point arithmetics. Use bc or awk:
Code:
price=95.3 
price1=$(echo "(20/100)*$price+$price" | bc -l)

This User Gave Thanks to mirni For This Post:
# 3  
Old 01-05-2012
Code:
$ price1=`echo "scale=2; (20 / 100 * $price) + $price" | bc -l`
$ echo $price1
114.36

---------------------------------
Late post. Already replied by mirni.

Last edited by balajesuri; 01-05-2012 at 06:04 AM.. Reason: Late post
This User Gave Thanks to balajesuri For This Post:
# 4  
Old 01-05-2012
In awk ..
Code:
$ echo "" | nawk -v price=95.3 '{print (20/100)*price+price}'
114.36

This User Gave Thanks to jayan_jay For This Post:
# 5  
Old 01-05-2012
Thanks to all :-)
# 6  
Old 01-05-2012
Hi.

Suppose we have on file z1:
Code:
float price price1
price=95.3
price1="(20./100.)*$price+$price"
echo "price=$price1"

then zsh z1 produces:
Code:
% zsh z1
price=1.143600000e+02

and ksh z1 produces:
Code:
% ksh z1
price=114.36

For this context:
Code:
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
zsh 4.3.6
ksh 93s+

See man zsh and man ksh (long man pages). Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mathematical Operations on Column

Hi All, I want to perform a mathematical operation on column. Can anyone please help? Here is the sample of operation to be performed: 123 996 100 123 996 200 123 996 200 2015-09-21 123 996 100 123 996 200 123 996 100 What I want is to multiple all values of column # 3 by 100 and... (3 Replies)
Discussion started by: Zaib
3 Replies

2. Shell Programming and Scripting

Number calculations

I'm writing a script that will read all the fields of a text file into an array(if they are numeric), while at the same time computing the minimum and maximum values from the file. After that I want to output the average of all the numbers in the array. The first problem I'm having is that many... (10 Replies)
Discussion started by: ksmarine1980
10 Replies

3. Shell Programming and Scripting

Mathematical calculations using shell

Dear All, I read some variables in a file and assigned as name for each of them. If I do echo I am able to see the values as 1.0E-05,3.4,5.0E-03 etc, Now I want to do some mathematical operations with them. Lets say 1 1.0E-05*5.0E-03 expected ans is 5.0E-08 2 1.0E-05/5.0E-03 expected... (9 Replies)
Discussion started by: linuxUser_
9 Replies

4. Homework & Coursework Questions

Help mathematical shell programming

Hello Guys,For my homework I must write a shell script to do this serie, http://upload.wikimedia.org/math/f/8/f/f8f543d9ecd01c4ecca2a0b7bc1234a2.pngI know that I must use the "bc" for that, but for the script's itself i have no idea,(beginner) Can you plz just help me for have some idea?... (1 Reply)
Discussion started by: hamed.samie
1 Replies

5. Homework & Coursework Questions

Mathematical scripting question

Hello Guys,For my homework I must write a shell script to do this serie, http://upload.wikimedia.org/math/f/8/f/f8f543d9ecd01c4ecca2a0b7bc1234a2.pngI know that I must use the "bc" for that, but for the script's itself i have no idea,(beginner) Can you plz just help me for have some idea?... (1 Reply)
Discussion started by: hamed.samie
1 Replies

6. Shell Programming and Scripting

Mathematical Loop

Hi, I'm creating a loop that allows the user to enter any number, then their choice of operator and then another number until their operator choice is equal to = But I am getting an error saying integer expression expected. Any explanation on why this is happening? echo "Please enter a number"... (1 Reply)
Discussion started by: Addman1991
1 Replies

7. Shell Programming and Scripting

Error in mathematical calculation using bc

I am getting the error: Runtime error (func=(main), adr=10): Divide by zero When executing the mathematical expression: echo "scale=2; 1-(0/0)"|bc How to overcome this? (5 Replies)
Discussion started by: proactiveaditya
5 Replies

8. UNIX for Dummies Questions & Answers

Date Calculations

I need to be able to use the current date and calculate 7 days ago to be stored in another variable to be passed to a file in my Unix shell script. I need the date in the following format: date '+%m/%d/%Y' or 05/16/2006 How do I calculate date minus 7 days or 1 week ago? (8 Replies)
Discussion started by: mitschcg
8 Replies

9. Shell Programming and Scripting

ksh, calculations using bc

hi all, was wondering if there is another way to do calculations in ksh scripts other than using bc ?? i am using a script to calculate average response time and my script errors out after running for a bit. e.g code i am using : averageTime=$(print "$totalTime / $numberOfEntries" |... (2 Replies)
Discussion started by: cesarNZ
2 Replies

10. UNIX for Dummies Questions & Answers

Float calculations

As expr is used for integer calculations, which command is used for float calculations. (1 Reply)
Discussion started by: sharmavr
1 Replies
Login or Register to Ask a Question