我试图在Bash脚本中划分两个图像宽度,但Bash给我0作为结果:
RESULT=$(($IMG_WIDTH/$IMG2_WIDTH))
我确实研究了Bash指南,我知道我应该使用bc,在互联网上的所有例子中,他们都使用bc。在echo中,我试图把同样的东西放在我的SCALE中,但它不起作用。
以下是我在教程中找到的例子:
echo "scale=2; ${userinput}" | bc
我怎么能让巴斯给我0.5这样的浮点数呢?
我试图在Bash脚本中划分两个图像宽度,但Bash给我0作为结果:
RESULT=$(($IMG_WIDTH/$IMG2_WIDTH))
我确实研究了Bash指南,我知道我应该使用bc,在互联网上的所有例子中,他们都使用bc。在echo中,我试图把同样的东西放在我的SCALE中,但它不起作用。
以下是我在教程中找到的例子:
echo "scale=2; ${userinput}" | bc
我怎么能让巴斯给我0.5这样的浮点数呢?
当前回答
正如其他人指出的那样,bash没有内置的浮点运算符。
您可以在bash中实现浮点数,甚至不使用bc和awk之类的计算器程序,或者任何与此相关的外部程序。
我在我的项目shellmath中正是这样做的,分三个基本步骤:
把数字分成整数部分和小数部分 使用内置的整数运算符分别处理各个部分,同时注意位值和进位 重新组合结果
作为一个挑逗,我添加了一个演示脚本,使用以x=0为中心的泰勒级数计算e。
如果您有时间,请查看一下。欢迎您的反馈!
其他回答
如果你找到了你喜欢的变体,你也可以把它包装到一个函数中。
这里我将一些bashism包装到div函数中:
一个衬套:
function div { local _d=${3:-2}; local _n=0000000000; _n=${_n:0:$_d}; local _r=$(($1$_n/$2)); _r=${_r:0:-$_d}.${_r: -$_d}; echo $_r;}
或多行:
function div {
local _d=${3:-2}
local _n=0000000000
_n=${_n:0:$_d}
local _r=$(($1$_n/$2))
_r=${_r:0:-$_d}.${_r: -$_d}
echo $_r
}
现在你得到了这个函数
div <dividend> <divisor> [<precision=2>]
然后像这样使用它
> div 1 2
.50
> div 273 123 5
2.21951
> x=$(div 22 7)
> echo $x
3.14
更新 我添加了一个小脚本,为您提供了bash的基本浮点数操作:
用法:
> add 1.2 3.45
4.65
> sub 1000 .007
999.993
> mul 1.1 7.07
7.7770
> div 10 3
3.
> div 10 3.000
3.333
这里是脚本:
#!/bin/bash
__op() {
local z=00000000000000000000000000000000
local a1=${1%.*}
local x1=${1//./}
local n1=$((${#x1}-${#a1}))
local a2=${2%.*}
local x2=${2//./}
local n2=$((${#x2}-${#a2}))
local n=$n1
if (($n1 < $n2)); then
local n=$n2
x1=$x1${z:0:$(($n2-$n1))}
fi
if (($n1 > $n2)); then
x2=$x2${z:0:$(($n1-$n2))}
fi
if [ "$3" == "/" ]; then
x1=$x1${z:0:$n}
fi
local r=$(($x1"$3"$x2))
local l=$((${#r}-$n))
if [ "$3" == "*" ]; then
l=$(($l-$n))
fi
echo ${r:0:$l}.${r:$l}
}
add() { __op $1 $2 + ;}
sub() { __op $1 $2 - ;}
mul() { __op $1 $2 "*" ;}
div() { __op $1 $2 / ;}
您需要多精确的输出?如果你的用例已经可以接受通过bin的近似值,你甚至可以更进一步,利用POSIX退出码[0:256)(所有其他整数修改回该范围)。
在gawk/nawk/mawk-1中,它已经给了我epoch秒到整数级别,但我想扩展它以提取近毫秒的精度,但不是过分迂迂的,我在POSIX shell中运行这个命令
exit $(( 10#` gdate +%5N ` * 256 / 100000 ))
直接分配一个表示0的5位整数。将gnu-date的XXXXXX输出到256个bin中的1个,然后在awk获得system()调用的退出码(即所选的bin #)后撤销这256个bin。我发现这种方法比使用完整的getline调用开销更低。
该方法还直接将输出捕获到POSIX退出码中,而不是打印出一个额外的终端。
(如果以这种方式编写,shell算术自动将其转换为整数而不是* 0.0256)。把它们放在awk函数中,就像这样。10#强制以10为基数,以防止posix shell将“01733”解释为八进制数。
function msecs() { # n x 2**-8 = n divided by 256
return 2^-8 * \
system( "exit \44\50\50 " \
" 10\43\140 gdate \53" \
"%5N\140 \52 " \
"256 \57 100000 \51\51" )
}
对于我自己的代码,我应用了另外0.6%的理发来考虑shell开销。
** bash/shell中的注入安全浮点数学
注意:这个回答的重点是为在bash(或其他shell)中执行数学的注入安全解决方案提供思路。当然,同样也可以使用,只需稍作调整即可执行高级字符串处理等。
所提出的大多数解决方案都是使用外部数据(变量、文件、命令行、环境变量)动态构造小脚本。外部输入可用于向引擎中注入恶意代码,其中很多是恶意代码
下面是使用各种语言进行基本数学计算的比较,其中结果为浮点数。它计算A + B * 0.1(作为浮点数)。
所有的解决方案都试图避免创建难以维护的动态脚本,而是使用静态程序,并将参数传递给指定的变量。它们将安全地处理具有特殊字符的参数-减少代码注入的可能性。例外是'BC',它不提供输入/输出功能
例外是'bc',它不提供任何输入/输出,所有数据都来自stdin中的程序,所有输出都到stdout。所有计算都在沙箱中执行,不允许出现副作用(打开文件等)。理论上,注射是安全的设计!
A=5.2
B=4.3
# Awk: Map variable into awk
# Exit 0 (or just exit) for success, non-zero for error.
#
awk -v A="$A" -v B="$B" 'BEGIN { print A + B * 0.1 ; exit 0}'
# Perl
perl -e '($A,$B) = @ARGV ; print $A + $B * 0.1' "$A" "$B"
# Python 2
python -c 'import sys ; a = float(sys.argv[1]) ; b = float(sys.argv[2]) ; print a+b*0.1' "$A" "$B"
# Python 3
python3 -c 'import sys ; a = float(sys.argv[1]) ; b = float(sys.argv[2]) ; print(a+b*0.1)' "$A" "$B"
# BC
bc <<< "scale=1 ; $A + $B * 0.1"
如何在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
There are scenarios in wich you cannot use bc becouse it might simply not be present, like in some cut down versions of busybox or embedded systems. In any case limiting outer dependencies is always a good thing to do so you can always add zeroes to the number being divided by (numerator), that is the same as multiplying by a power of 10 (you should choose a power of 10 according to the precision you need), that will make the division output an integer number. Once you have that integer treat it as a string and position the decimal point (moving it from right to left) a number of times equal to the power of ten you multiplied the numerator by. This is a simple way of obtaining float results by using only integer numbers.