I have a backup script on my server which does cron jobs of backups, and sends me a summary of files backed up, including the size of the new backup file. As part of the script, I'd like to divide the final size of the file by (1024^3) to get the file size in GB, from the file size in bytes.
Since bash does not have floating point calculation, I am trying to use pipes to bc to get the result, however I'm getting stumped on basic examples.
I tried to get the value of Pi to a scale, however,
even though the following works:
~ #bc -l bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 4/3 1.33333333333333333333 22/7 3.14285714285714285714 q 0 quit
A non interactive version does not work:
#echo $(( 22/7 )) | bc 3
This works:
#echo '22/7' | bc -l 3.14285714285714285714
But I need to use variables. So it doesnt help that the following does not work:
#a=22 ; b=7 #echo $(( a/b )) | bc -l 3
I'm obviously missing something in the syntax for using variables in Bash, and could use with some 'pointers' on what I've misunderstood.
As DigitalRoss said, I can use the following:
#echo $a / $b | bc -l 3.14285714285714285714
However I cant use complex expressions like:
#echo $a / (( $b-34 )) | bc -l -bash: syntax error near unexpected token `(' #echo $a / (( b-34 )) | bc -l -bash: syntax error near unexpected token `(' #echo $a / (( b-34 )) | bc -l -bash: syntax error near unexpected token `('
Can someone give me a working correct syntax for getting floating point results with complicated arithmetic expresssions?
While you can't use floating point division in Bash you can use fixed point division. All that you need to do is multiply your integers by a power of 10 and then divide off the integer part and use a modulo operation to get the fractional part. Rounding as needed.
Conclusion. In this article, we discussed several ways of performing division in Bash. We saw that the expr, let, and double-parentheses compound commands allow only integer division, and the result is also an integer.
To divide float values in Python, use the / operator. The Division operator / takes two parameters and returns the float division. Float division produces a floating-point conjecture of the result of a division. If you are working with Python 3 and you need to perform a float division, then use the division operator.
Although Bash arithmetic expansion does not support floating-point arithmetic, there are other ways to perform such calculations. Below are four examples using commands or programming languages available on most Linux systems.
Just double-quote ("
) the expression:
echo "$a / ( $b - 34 )" | bc -l
Then bash will expand the $
variables and ignore everything else and bc
will see an expression with parentheses:
$ a=22 $ b=7 $ echo "$a / ( $b - 34 )" 22 / ( 7 - 34 ) $ echo "$a / ( $b - 34 )" | bc -l -.81481481481481481481
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With