Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
946 views
in Technique[技术] by (71.8m points)

bc - Floating point results in Bash integer division

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...