Bash:评估一个数学术语?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2552130/
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 19:06:48  来源:igfitidea点击:

Bash: evaluate a mathematical term?

bashmathshevaluation

提问by hhh

echo 3+3

How can I evaluate such expressions in Bash, in this case to 6?

如何在 Bash 中评估此类表达式,在这种情况下为 6?

采纳答案by ghostdog74

in shells such as zsh/ksh, you can use floats for maths. If you need more maths power, use tools like bc/awk/dc

在诸如 zsh/ksh 之类的 shell 中,您可以使用浮点数进行数学运算。如果您需要更多的数学能力,请使用诸如bc/awk/dc

eg

例如

var=$(echo "scale=2;3.4+43.1" | bc)
var=$(awk 'BEGIN{print 3.4*43.1}')

looking at what you are trying to do

看看你正在尝试做什么

awk '{printf "%.2f\n",
echo $(( 3+3 ))
/59.5}' ball_dropping_times >bull_velocities

回答by Marcelo Cantos

$((3+3))  returns 6
((3+3))   used in conditionals, returns 0 for true (non-zero) and 1 for false
let 3+3   same as (( ))

回答by Dan Andreatta

expris the standard way, but it only handles integers.

expr是标准方式,但它只处理整数。

bash has a couple of extensions, which only handle integers as well:

bash 有几个扩展,它们也只处理整数:

let a=3+3
((a=3+3))

letand (( ))can be used to assign values, e.g.

let并且(( ))可用于分配值,例如

expr 3 + 3

for floating point you can use bc

对于浮点,您可以使用 bc

echo 3+3 | bc

回声 3+3 | 公元前

回答by codaddict

You can make use of the exprcommand as:

您可以将expr命令用作:

sum=$(expr 3 + 3)

To store the result into a variable you can do:

要将结果存储到变量中,您可以执行以下操作:

sum=`expr 3 + 3`

or

或者

expr 3 + 3

回答by codaddict

Lots of ways - most portable is to use the exprcommand:

很多方法 - 最便携的是使用expr命令:

$ cat calc_velo.sh

#!/bin/bash

for i in `cat ball_dropping_times`
do
echo "scale=20; $i / 59.5" | bc 
done > ball_velocities

回答by The HCD

I believe the ((3+3)) method is the most rapid as it's interpreted by the shell rather than an external binary. time a large loop using all suggested methods for the most efficient.

我相信 ((3+3)) 方法是最快速的,因为它是由 shell 而不是外部二进制文件解释的。使用所有建议的方法为最有效的大循环计时。

回答by hhh

Solved thanks to Dennis, an example of BC-use:

感谢 Dennis 解决了,一个 BC 使用的例子:

bashj +eval "3+3"
bashj +eval "3.5*5.5"

回答by Fil

My understanding of math processing involves floating point processing.

我对数学处理的理解涉及浮点处理。

Using bashj(https://sourceforge.net/projects/bashj/) you can call a java method (with floating point processing, cos(), sin(), log(), exp()...) using simply

使用bashj( https://sourceforge.net/projects/bashj/),您可以使用简单的方法调用 java 方法(带浮点处理、cos()、sin()、log()、exp()...)

#!/usr/bin/bashj
EXPR="3.0*6.0"
echo $EXPR "=" u.doubleEval($EXPR)

FUNCTIONX="3*x*x+cos(x)+1"
X=3.0
FX=u.doubleEval($FUNCTIONX,$X)
echo "x="$X " => f(x)=" $FUNCTIONX "=" $FX

or in a bashj script, java calls of this kind:

或者在 bashj 脚本中,这种 java 调用:

##代码##

Note the interesting speed : ~ 10 msec per call (the answer is provided by a JVM server).

请注意有趣的速度:每次调用约 10 毫秒(答案由 JVM 服务器提供)。

Note also that u.doubleEval(1/2)will provide 0.5(floating point) instead of 0(integer)

另请注意,u.doubleEval(1/2)将提供0.5(浮点数)而不是0(整数)

回答by codarrior

One use case that might be useful in this regard is, if one of your operand itself is a bashcommand then try this.

在这方面可能有用的一个用例是,如果您的操作数本身之一是bash命令,请尝试此操作。

echo $(( `date +%s\`+10 ))or even echo $(( `date +%s\`+(60*60) ))

echo $(( `date +%s\`+10 ))甚至 echo $(( `date +%s\`+(60*60) ))

In my case I was trying to get Unixtime 10 seconds and hour later than current time respectively.

就我而言,我试图分别将 Unixtime 比当前时间晚 10 秒和 1 小时。