Mathematical calculations using shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Mathematical calculations using shell
# 1  
Old 09-06-2014
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
Code:
1.0E-05*5.0E-03

expected ans is
Code:
5.0E-08

2
Code:
1.0E-05/5.0E-03

expected ans is
Code:
2.0E-03

How can I do that?

My trails:
Code:
abcd='echo "$DT+$WI"| bc -l';
echo $abcd;

Code:
prnt=`echo "scale=8; ($DT * $WI) + $ET" | bc -l`
echo $prnt

Here DT=1.0E-05,ET=3.4,WI=5.0E-03
None of them worked.

Thanks & Regards,
linuxUser_
# 2  
Old 09-06-2014
Perhaps using AWK? You mentioned shell, AWK is external, not sure if this will work for you.

Code:
jaysunn-> awk '{print $1 * $2}' <<< '1.0E-05 5.0E-03'
5e-08

# 3  
Old 09-06-2014
Quote:
Originally Posted by jaysunn
Perhaps using AWK? You mentioned shell, AWK is external, not sure if this will work for you.

Code:
jaysunn-> awk '{print $1 * $2}' <<< '1.0E-05 5.0E-03'
5e-08

Can I use variables like $DT, $WI???

actually I am reading these values from other file and assigning to variables DT and WI.

I don't want to specify values directly.
# 4  
Old 09-06-2014
You could also try:
bash:
Code:
printf "%e\n" "$(bc -l <<< "${DT/E/*10^}*${WI/E/*10^}" )"

# 5  
Old 09-06-2014
Quote:
Originally Posted by Scrutinizer
You could try:
bash:
Code:
printf "%.e\n" "$(bc -l <<< "${DT/E/*10^}*${WI/E/*10^}" )"

Sorry that I may be didn't conveyed my msg properly.
Code:
cDir=`pwd`
while IFS=$IFS\; read name val
do
  case $name in
     expVal)
       EV=$val  ;;
     deltaVal)
       DV=$val  ;;
     wallVal)
       WV=$val  ;;
  esac
done < text.txt
echo $EV;
echo $DV;
echo $WV;

Output
Code:
10.0
5.0e-05
1.0e-05

But I am not able to do some math operations as $EV*$DV etc.
How can I do that?
# 6  
Old 09-06-2014
Yes that is what I mean, then use the proper variable and lowercase "e" :
Code:
printf "%e\n" "$(bc -l <<< "${EV/e/*10^}*${DV/e/*10^}" )"

Code:
$ EV=10.0 DV=5.0e-05 WV=1.0e-05
$ printf "%e\n" "$(bc -l <<< "${EV/e/*10^}*${DV/e/*10^}" )"
5.000000e-04


Last edited by Scrutinizer; 09-06-2014 at 01:32 PM..
# 7  
Old 09-06-2014
Quote:
Originally Posted by Scrutinizer
Yes that is what I mean, then use the proper variable and lowercase "e" :
Code:
printf "%e\n" "$(bc -l <<< "${EV/e/*10^}*${DV/e/*10^}" )"

Code:
$ EV=10.0 DV=5.0e-05 WV=1.0e-05
$ printf "%e\n" "$(bc -l <<< "${EV/e/*10^}*${DV/e/*10^}" )"
5.000000e-04

Excellent !!
Thanks.

But not done yet.
My number in this case is 5E-5, but it also can be 0.00005.
Irrespective of number type(scientific, number with specified precision) can we do math operations and compare them(I mean 0.00005, 5E-5, 5e-5, 5.0E-5 and 5.0e-5 should be treated as same)?

Thanks once again Smilie
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. 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

3. Shell Programming and Scripting

Help with Arithmetic calculations in Shell script

Hi, I need a help with arithmetic calculations in my script. I have two variables: a=17; b=1712 I want to perform ($a/$b)*100 with two decimals in the result. I tried with following: res=$((100*a/b)) res=`echo "scale=2; $a / $b" | bc` But I am not getting the decimal values.... (4 Replies)
Discussion started by: karumudi7
4 Replies

4. Shell Programming and Scripting

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 price=95.3 price1=(20/100)*$price+$price echo "price=$price1" finally the output should display price=114.36 Thanks (5 Replies)
Discussion started by: aish11
5 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Mathematical functions in bash shell

Hi, How can i do the mathematical calculations in bash shell? Are the mathematical functions available in bash shell? Ex: pow ceil floor sqrt (5 Replies)
Discussion started by: cola
5 Replies

8. Shell Programming and Scripting

shell execution time calculations

I am writting a script in the ksh shell and am trying to find a way to report the total execution time of the script without requiring the user to specify the time function when executing the script. Does anyone have any examples they have used. I have been setting up two date variables (one at... (1 Reply)
Discussion started by: Omar Bacha
1 Replies

9. Shell Programming and Scripting

shell script receiving variable and doing mathematical function

Hello, Please help for the following scenario: 1. Shell Scipt should receive 2 variables values (say a and b). 2. Within shell script, there should be division of those numbers (a/b). 3. The result should be whole number i.e. in case the result comes out to be 9.4, it should be returned as... (3 Replies)
Discussion started by: damansingh
3 Replies

10. Shell Programming and Scripting

problem doing the time calculations in shell.

I am trying to do a shell script to monitor if any files went through in the last hour. There is a script in cron that runs every sec checking to see if a file is there and ftp the file out of this folder. Now I just want to add a block of code that will check to see no files went in the last... (3 Replies)
Discussion started by: jonathan184
3 Replies
Login or Register to Ask a Question