Mathematical calculations using shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Mathematical calculations using shell
# 8  
Old 09-06-2014
Yes, try:
Code:
printf "%e\n" "$(bc -l <<< "${EV/[eE]/*10^}*${DV/[eE]/*10^}" )"


or, for example:
Code:
sci_bc() {
  printf "%e\n" "$(bc -l <<< "${1//[eE]/*10^}")"
}

sci_bc "$EV*$DV"


Last edited by Scrutinizer; 09-06-2014 at 01:59 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 09-06-2014
Quote:
Originally Posted by Scrutinizer
Yes, try:
Code:
printf "%e\n" "$(bc -l <<< "${EV/[eE]/*10^}*${DV/[eE]/*10^}" )"

Its working well. Great help Smilie

Two more questions,

1. How can I store it in another variable?
2. I am able to see 5.000000e-04. How can I change it to specified format like 5.0E-04 (or) 0.0005 (or)5.00E-4 etc,??
# 10  
Old 09-06-2014
Good to hear Smilie

1.
Code:
newvar=$(printf "%e\n" "$(bc -l <<< "${EV/[eE]/*10^}*${DV/[eE]/*10^}" )")

2.
Code:
$ printf "%e\n" "$(bc -l <<< "${EV/[eE]/*10^}*${DV/[eE]/*10^}" )"
5.000000e-04
$ printf "%.1E\n" "$(bc -l <<< "${EV/[eE]/*10^}*${DV/[eE]/*10^}" )"
5.0E-04
$ printf "%.4f\n" "$(bc -l <<< "${EV/[eE]/*10^}*${DV/[eE]/*10^}" )"
0.0005
$ printf "%.2E\n" "$(bc -l <<< "${EV/[eE]/*10^}*${DV/[eE]/*10^}" )"
5.00E-04
$

These 2 Users Gave Thanks to Scrutinizer For This Post:
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