我试图在Bash脚本中划分两个图像宽度,但Bash给我0作为结果:

RESULT=$(($IMG_WIDTH/$IMG2_WIDTH))

我确实研究了Bash指南,我知道我应该使用bc,在互联网上的所有例子中,他们都使用bc。在echo中,我试图把同样的东西放在我的SCALE中,但它不起作用。

以下是我在教程中找到的例子:

echo "scale=2; ${userinput}" | bc 

我怎么能让巴斯给我0.5这样的浮点数呢?


当前回答

bash

正如其他人指出的那样,bash不支持浮点运算,尽管您可以使用一些固定的小数技巧来伪造它,例如使用两个小数:

echo $(( 100 * 1 / 3 )) | sed -e 's/..$/.&/;t' -e 's/.$/.0&/'

输出:

.33

请参阅Nilfred的回答,了解类似但更简洁的方法。

选择

除了提到的bc和awk的替代品,还有以下:

clisp

clisp -x '(/ 1.0 3)'

清理输出:

clisp --quiet -x '(/ 1.0 3)'

或者通过stdin:

echo '(/ 1.0 3)' | clisp --quiet | tail -n1

dc

echo 2k 1 3 /p | dc

天才cli计算器

echo 1/3.0 | genius

ghostscript

echo 1 3 div = | gs -dNODISPLAY -dQUIET | sed -n '1s/.*>//p' 

gnuplot

echo 'pr 1/3.' | gnuplot

Imagemagick

convert xc: -format '%[fx:1/3]' info:

或者通过stdin:

echo 1/3 | { convert xc: -format "%[fx:$(cat)]" info:; }

jq

jq -n 1/3

或者通过stdin:

echo 1/3 | jq -nf /dev/stdin

ksh

echo 'print $(( 1/3. ))' | ksh

lua

lua -e 'print(1/3)'

或者通过stdin:

echo 'print(1/3)' | lua

maxima

echo '1/3,numer;' | maxima

清理输出:

echo '1/3,numer;' | maxima --quiet | sed -En '2s/[^ ]+ [^ ]+ +//p'

node

echo 1/3 | node -p

倍频程

echo 1/3 | octave

perl

echo print 1/3 | perl

python2

echo print 1/3. | python2

python3

echo 'print(1/3)' | python3

R

echo 1/3 | R --no-save

清理输出:

echo 1/3 | R --vanilla --quiet | sed -n '2s/.* //p'

ruby

echo puts 1/3.0 | ruby

单位

units 1/3

输出紧凑:

units --com 1/3

水垢

echo 1/3 | wcalc

清理输出:

echo 1/3 | wcalc | tr -d ' ' | cut -d= -f2

zsh

print $(( 1/3. ))

或者通过stdin:

echo 'print $(( 1/3. ))' | zsh

其他来源

Stéphane Chazelas在UL上回答了类似的问题。

其他回答

如何在bash中进行浮点计算:

不同于在bc命令中使用"here strings"(<<<),这是我最喜欢的bc浮点示例,来自bc手册页的示例部分(参见man bc手册页)。

在我们开始之前,知道pi的方程是:pi = 4*atan(1)。下面的A()是atan()的BC数学函数。

This is how to store the result of a floating point calculation into a bash variable--in this case into a variable called pi. Note that scale=10 sets the number of decimal digits of precision to 10 in this case. Any decimal digits after this place are truncated. pi=$(echo "scale=10; 4*a(1)" | bc -l) Now, to have a single line of code that also prints out the value of this variable, simply add the echo command to the end as a follow-up command, as follows. Note the truncation at 10 decimal places, as commanded: pi=$(echo "scale=10; 4*a(1)" | bc -l); echo $pi 3.1415926532 Finally, let's throw in some rounding. Here we will use the printf function to round to 4 decimal places. Note that the 3.14159... rounds now to 3.1416. Since we are rounding, we no longer need to use scale=10 to truncate to 10 decimal places, so we'll just remove that part. Here's the end solution: pi=$(printf %.4f $(echo "4*a(1)" | bc -l)); echo $pi 3.1416

下面是上述技术的另一个非常棒的应用程序和演示:测量和打印运行时。

(参见我的另一个答案)。

注意,dt_min从0.01666666666…0.017:

start=$SECONDS; sleep 1; end=$SECONDS; dt_sec=$(( end - start )); dt_min=$(printf %.3f $(echo "$dt_sec/60" | bc -l)); echo "dt_sec = $dt_sec; dt_min = $dt_min"
dt_sec = 1; dt_min = 0.017

相关:

(我的回答)https://unix.stackexchange.com/questions/52313/how-to-get-execution-time-of-a-script-effectively/547849#547849 [我的问题]三个左尖括号(' <<< ')在bash中是什么意思? https://unix.stackexchange.com/questions/80362/what-does-mean/80368#80368 https://askubuntu.com/questions/179898/how-to-round-decimals-using-bc-in-bash/574474#574474

您可以通过-l选项使用bc (L字母)

RESULT=$(echo "$IMG_WIDTH/$IMG2_WIDTH" | bc -l)

稍微改进一下马文的回答:

RESULT=$(awk "BEGIN {printf \"%.2f\",${IMG_WIDTH}/${IMG2_WIDTH}}")

BC并不总是安装包。

Bash可以很好地计算浮点结果,不需要任何其他程序。

Bash独立甚至可以精确地计算π到小数点后第九位。

例子:

calc=104348/33215

accuracy=9

calc99p9=$((10**$accuracy))*$calc
result99p9=$((calc99p9))
result=${result99p9: -${#result99p9}: -$accuracy}.${result99p9: -$accuracy}

echo Bash calculated pi to be $result

结果

Bash calculated pi to be 3.141592653

作为bc的替代方案,您可以在脚本中使用awk。

例如:

echo "$IMG_WIDTH $IMG2_WIDTH" | awk '{printf "%.2f \n", $1/$2}'

在上面,“%.”2f”告诉printf函数返回一个小数点后两位的浮点数。我使用echo将变量管道作为字段,因为awk对它们正常操作。“$1”和“$2”表示输入awk的第一个和第二个字段。

你可以将结果存储为其他变量,使用:

RESULT = `echo ...`