Arithmetic calculations in bash file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arithmetic calculations in bash file
# 1  
Old 03-03-2013
Arithmetic calculations in bash file

I have 2 numbers

Code:
xmin = 0.369000018
xmax = 0.569000006

and want to calculate

Code:
(xmax- xmin) / 5.0

I have tried using $(( )) but is always giving an error
# 2  
Old 03-03-2013
Here is an approach using bc
Code:
#!/bin/bash

xmin=0.369000018
xmax=0.569000006

echo "scale=9; ( $xmax - $xmin ) / 5.0" | bc

# 3  
Old 03-03-2013
Quote:
Originally Posted by kristinu
I have 2 numbers

Code:
xmin = 0.369000018
xmax = 0.569000006

and want to calculate

Code:
(xmax- xmin) / 5.0

I have tried using $(( )) but is always giving an error
The bash shell doesn't perform non-integer arithmetic.
You can do it with a recent (newer than 1988) Korn shell:
Code:
xmin=0.369000018
xmax=0.569000006
printf "%.10f\n" $(( (xmax - xmin) / 5.0))

which produces:
Code:
0.0399999976

# 4  
Old 03-03-2013
Bugger. I'm stuck with bash.

---------- Post updated at 07:53 PM ---------- Previous update was at 07:49 PM ----------

I it possible to write a function to do a calculation using bc?

---------- Post updated at 08:02 PM ---------- Previous update was at 07:53 PM ----------

Seems a solution would be in awk

---------- Post updated at 08:04 PM ---------- Previous update was at 08:02 PM ----------

This did not work

Code:
compute() {

  echo "(0.569000006-0.369000018)/5.0" | awk '{ print $1 }'

}

# 5  
Old 03-03-2013
Code:
awk ' BEGIN { printf "%f\n", (0.569000006-0.369000018) / 5.0 } '

# 6  
Old 03-03-2013
Aney idea what's the problem with this?

Code:
awk -v a=0.569000006 -v b=0.369000018 -v c=5.0 '{res= (a-b)/c; print res;}'

# 7  
Old 03-03-2013
You need a BEGIN block which is executed once before any input is read. In this case BEGIN block runs and program exits:
Code:
awk -v a=0.569000006 -v b=0.369000018 -v c=5.0 ' BEGIN {res= (a-b)/c; print res;}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Arithmetic with bash

I need to divide the number of white spaces by total number of characters in a file using bash. I am able to get the number of white spaces correctly using: tr -cd < afile | wc -c I am also able to get the total number of characters using: wc -c afile How do I divide the first... (2 Replies)
Discussion started by: ngabrani
2 Replies

2. Shell Programming and Scripting

Bash arithmetic issue

I found the following issue by simply increasing a variable. The ((A++)) expression returns an error, the other expressions both return 0. Does anyone know why? script.sh: #! /bin/bash A=0 B=0 C=0 ((A++)) ; echo "${?}" ((B=B+1)) ; echo "${?}" ((C+=1))... (8 Replies)
Discussion started by: elbrand
8 Replies

3. Shell Programming and Scripting

Arithmetic on a Float in bash

I am using bash I have a script that takes a number, i.e. 85.152, which is always a non integer and essentially tries to get that number to be a multiple of 10. My code is as follows: number=85.152 A=${number%.*} #Converts float to integer typeset -i B=$(((A-20)/10)) #subtracting 20 is... (12 Replies)
Discussion started by: prodigious8
12 Replies

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

5. Shell Programming and Scripting

bash script range calculations

Hi, I have data in the following form: AB001 10 AB002 9 AB003 9 etc AB200 5 What I need to do is sum up the second value according to groups of the first, i.e. AB001 to AB030 the total being X, AB031 to AB050 the total being Y etc (there are 5 AB ranges of different sizes). I'm sure... (3 Replies)
Discussion started by: chrissycc
3 Replies

6. Shell Programming and Scripting

Arithmetic operations in bash,ksh,sh

Guys, The below expression is valid in which shells (sh,ksh,bash,csh)? VAR1=2 VAR2=$(($VAR1 -2)) Thanks (1 Reply)
Discussion started by: rprajendran
1 Replies

7. Shell Programming and Scripting

calculations in bash

HI i have following problem, i need to use split command to split files each should be cca 700 lines but i dont know how to inplement it in the scripts becasuse each time the origin file will be various size , any body got any idea cheers (2 Replies)
Discussion started by: kvok
2 Replies

8. Shell Programming and Scripting

Need help is manipulating a file with some arithmetic operations using bash script

Friends, I have a file with contents like: interface Serial0/4/0/0/1/1/1/1:0 encapsulation mfr multilink group 101 Now I need to manipulate the file in such a way that to all the numbers less than 163, 63 gets added and to all numbers greater than 163, 63 gets deducted.(The numbers... (2 Replies)
Discussion started by: shrijith1
2 Replies

9. Shell Programming and Scripting

any way to speed up calculations in bash script

hi i have a script that is taking the difference of multiple columns in a file from a value from a single row..so far i have a loop to do that.. all the data is floating point..fin has the difference between array1 and array2..array1 has 700 x 300= 210000 values and array2 has 700 values.. ... (11 Replies)
Discussion started by: npatwardhan
11 Replies

10. Shell Programming and Scripting

Non-integer calculations in bash

I'm new at scripting but I thought I was getting pretty good at it. I've hit a snag. I try to use expr to compute a fraction say: expr 3 / 4, and I'm getting zero. I guess it's just truncating to the integer, in this case 0, but I need the decimal 0.75. What can I do to compute this value in... (2 Replies)
Discussion started by: jeriryan87
2 Replies
Login or Register to Ask a Question