浮点数导致 Bash 整数除法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15015809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:21:20  来源:igfitidea点击:

Floating point results in Bash integer division

bashbc

提问by Joel G Mathew

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.

我的服务器上有一个备份脚本,它执行备份的 cron 作业,并向我发送备份文件的摘要,包括新备份文件的大小。作为脚本的一部分,我想将文件的最终大小除以 (1024^3) 以从文件大小(以字节为单位)中得到以 GB 为单位的文件大小。

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.

由于 bash 没有浮点计算,我试图使用管道到 bc 来获得结果,但是我对基本示例感到困惑。

I tried to get the value of Pi to a scale, however,

然而,我试图将 Pi 的值调整到一定程度,

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.

我显然在 Bash 中使用变量的语法中遗漏了一些东西,并且可以使用一些我误解的“指针”。

As DigitalRoss said, I can use the following:

正如 DigitalRoss 所说,我可以使用以下内容:

#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?

有人可以给我一个正确的语法来使用复杂的算术表达式获得浮点结果吗?

采纳答案by Adrian Pronk

Just double-quote (") the expression:

只需双引号 ( ") 表达式:

echo "$a / ( $b - 34 )" | bc -l

Then bash will expand the $variables and ignore everything else and bcwill see an expression with parentheses:

然后 bash 将扩展$变量并忽略其他所有内容,bc并将看到一个带括号的表达式:

$ a=22
$ b=7
$ echo "$a / ( $b - 34 )" 
22 / ( 7 - 34 )

$ echo "$a / ( $b - 34 )" | bc -l
-.81481481481481481481

回答by user834425

Please note that your echo $(( 22/7 )) | bc -lactually makes bash calculate 22/7 and then send the result to bc. The integer output is therefore not the result of bc, but simply the input given to bc.

请注意,您echo $(( 22/7 )) | bc -l实际上使 bash 计算 22/7,然后将结果发送到 bc。因此整数输出不是 bc 的结果,而只是给 bc 的输入。

Try echo $(( 22/7 ))without piping it to bc, and you'll see.

尝试echo $(( 22/7 ))不通过管道将其发送到 bc,您会看到。