BASH算术表达式 [英] BASH Arithmetic Expressions

查看:205
本文介绍了BASH算术表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在BASH(3.2)中使用了几种方法来做一些简单的整数运算。但是我无法找出最好的(首选)方法。

  result =`expr 1 + 2` 
result = $((1 + 2))
letresult = 1 + 2

这些表达式之间的根本区别是什么?

还有其他方式做同样的事情吗?



是否使用工具像 bc 必须是浮点算术?

  result =`echo 7/354| bc` 


解决方案

在Bash中,允许一行上的多个作业:

  let a = 3 b = 4 c = 5 

正如您在问题中显示的那样,引用参数 let 允许您在操作员周围放置空格。但是,如果您避免使用空格,您可以省略引号。



在语句开头使用双括号的另一个表单(而不是 i = $((j + 1))表单)允许您在等号周围包含空格,或执行后期或预增量或递减和附加分配操作:

 ((a =(b + c)* 4))
((count ++))
((d = --c ** b $ b((e * = 2))
((f = 3,g = 5))#多个操作需要一个逗号分隔符

如果你做帮助((它说双括号是等于 让EXPRESSION



您可以使用声明内置作业,包括间接的:

  blue = 2 
name = blue
declare $ name = 4
echo $ blue#result:4
echo $ {!name}#result:4

编辑:



$(() )构造被称为算术扩展,并使内容被评估为整数表达式。它是shell的语法元素。



如果一个变量被声明为一个整数,则不需要使用任何一种双括号,可以省略美元符号从变量名称(如双括号表单中),但您不能在运算符周围添加空格:

  declare  - ix = 1#set integer,initialize to 1 
declare + is = 1#clear integer,initialize to 1
x + = 1#也可以是x = x + 1
echo $ x#结果:2(加)
s + = 1#也可以是s = $ s + 1,需要一个$
echo $ s#result:11(string concatenation)

与上述表单不同,调用 expr 涉及产生外部可执行文件,对于循环中的大量计算来说是相当昂贵的。应该使用的 时间是在shell无法做到自己的算术或便携性的环境中,当脚本可能会进入这样的环境时。 POSIX shell具有算术能力,因此只能使用较旧的系统。



关于使用 bc 当使用Bash和许多其他shell时,需要使用浮点算术或类似的东西。 POSIX说只需要签名的长整数算术。



支持浮点数学的两个shell是ksh和zsh。除了 bc ,您可以在Bash脚本中使用 dc ,AWK,Python,Perl等。 / p>

Bash 将使用浮点数编号的一件事情是用 printf 内建(请注意,还有一个外部 printf ,但内置优先级)。

  printf%'14 .4f\\\
1234.56#result1,234.5600(在我的地区)


I had used several ways to do some simple integer arithmetic in BASH (3.2). But I can't figure out the best (preferred) way to do it.

result=`expr 1 + 2`
result=$(( 1 + 2 ))
let "result = 1 + 2"

What are the fundamental differences between those expressions?
Is there other ways to do the same?

Is the use of a tool like bc mandatory for floating point arithmetic?

result=`echo "7/354" | bc`

解决方案

In Bash, let allows multiple assignments on a line:

let a=3 b=4 c=5

As you show in your question, quoting the argument to let allows you to put spaces around the operators. You can, however, omit the quotes if you avoid using spaces.

Another form using double parentheses at the beginning of the statement (instead of the i=$((j + 1)) form) allows you to include spaces around the equal sign or do post- or pre- increment or decrement and additional assignment operations:

(( a = ( b + c ) * 4 ))
(( count++ ))
(( d = --c**2 ))
(( e *= 2 ))
(( f = 3, g = 5 ))    # multiple operations require a comma separator

If you do help "((" it says that the double parentheses is 'Equivalent to "let EXPRESSION".'

You can use the declare builtin to make assignments, including indirectly:

blue=2
name=blue
declare $name=4
echo $blue    # result: 4
echo ${!name} # result: 4

Edit:

The $(()) construct is called "arithmetic expansion" and causes the contents to be evaluated as an integer expression. It's a syntax element of the shell.

If a variable is declared as an integer you don't need to use either form of double parentheses, you can omit the dollar sign from the variable name (as in the double-parentheses forms), but you can't add spaces around operators:

declare -i x=1   # set integer, initialize to 1
declare +i s=1   # clear integer, initialize to 1
x+=1             # could also be x=x+1
echo $x          # result: 2 (addition)
s+=1             # could also be s=$s+1, requires a "$"
echo $s          # result: 11 (string concatenation)

Unlike the forms above, calling expr involves spawning an external executable which can be quite expensive for a lot of calculations in a loop. The only time it should be used is in environments where the shell can't do its own arithmetic or for portability when a script may find its way into such an environment. POSIX shells have arithmetic capability so it would be a concern only with older systems.

Regarding the use of bc for floating point arithmetic, it or something similar is required when using Bash and many other shells. POSIX says that "Only signed long integer arithmetic is required."

Two shells that do support float math are ksh and zsh. In addition to bc, you can use dc, AWK, Python, Perl and others from within a Bash script.

One thing that Bash will do with floating point numbers is print them with the printf builtin (note that there is also an external printf, but builtins have priority).

printf "%'14.4f\n" 1234.56  # result "    1,234.5600" (in my locale)

这篇关于BASH算术表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆