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

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

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

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

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

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


当前回答

虽然在Bash中不能使用浮点除法,但可以使用定点除法。你所要做的就是把整数乘以10的次方,然后把整数部分除掉,用取模运算得到小数部分。根据需要舍入。

#!/bin/bash

n=$1
d=$2

# because of rounding this should be 10^{i+1}
# where i is the number of decimal digits wanted
i=4
P=$((10**(i+1)))
Pn=$(($P / 10))
# here we 'fix' the decimal place, divide and round tward zero
t=$(($n * $P / $d + ($n < 0 ? -5 : 5)))
# then we print the number by dividing off the interger part and
# using the modulo operator (after removing the rounding digit) to get the factional part.
printf "%d.%0${i}d\n" $(($t / $P)) $(((t < 0 ? -t : t) / 10 % $Pn))

其他回答

如何在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

现在是尝试zsh的最佳时机,它是一个(几乎)bash超集,具有许多其他不错的特性,包括浮点数学。下面是你的例子在zsh中的样子:

% IMG_WIDTH=1080
% IMG2_WIDTH=640
% result=$((IMG_WIDTH*1.0/IMG2_WIDTH))
% echo $result
1.6875

这篇文章可能会帮助你:bash -值得切换到zsh随意使用?

用calc,这是我发现的最简单的方法 例子:

calc 1 + 1

 2

calc 1/10

 0.1

在浮点数出现之前,固定小数逻辑是被使用的:

IMG_WIDTH=100
IMG2_WIDTH=3
RESULT=$((${IMG_WIDTH}00/$IMG2_WIDTH))
echo "${RESULT:0:-2}.${RESULT: -2}"
33.33

最后一行是bashim,如果不使用bash,试试下面的代码:

IMG_WIDTH=100
IMG2_WIDTH=3
INTEGER=$(($IMG_WIDTH/$IMG2_WIDTH))
DECIMAL=$(tail -c 3 <<< $((${IMG_WIDTH}00/$IMG2_WIDTH)))
RESULT=$INTEGER.$DECIMAL
echo $RESULT
33.33

代码背后的基本原理是:在除之前乘以100得到两个小数。

你可以这样做:

bc <<< 'scale=2; 100/3'
33.33

更新20130926:你可以使用:

bc -l <<< '100/3' # saves a few hits
33.33333333333333333333