BASH算术防爆pressions [英] BASH Arithmetic Expressions

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

问题描述

我用了几个方面做一些简单的整数运算的BASH(3.2)。但我不能找出最佳的(preferred)的方式来做到这一点。

 的结果=`expr的1 + 2`
结果= $((1 + 2))
让的结果= 1 + 2

什么是那些前pressions之间的根本区别是什么?结果
是否有其他方法可以做?

时的BC 使用像工具的强制浮点运算?

 的结果=`回声三百五十四分之七| bc`


解决方案

在bash中,允许在一条线上的多个作业:

 让= 3 B = 4,C = 5

当你在你的问题表明,引述参数允许你把周围的经营空间。你可以,但是,省略了引号,如果您避免使用空间。

使用双括号在声明的开头另一种形式(而不是 I = $((J + 1))形式),可以包括周围的空间等号或做后或pre型递增或递减的和额外的赋值操作:

 ((A =(B + C)* 4))
((计数++))
((D = --c ** 2))
((E * = 2))
((F = 3,G = 5))#多个操作需要一个逗号分隔符

如果您帮助((它说,在双括弧是等价于让EX preSSION

您可以使用声明内建进行分配,包括间接的:

 蓝色= 2
名称=蓝
申报$ NAME = 4
回声$#蓝色结果:4
回声$#{结果名字!}:4

编辑:

$(())构造被称为<一个href=\"http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04\">arithmetic扩张并且使得内容作为整数前pression进行评估。它的外壳的语法元素。

如果一个变量声明为整数你不需要使用双括号的两种形式,可以忽略美元符号从变量名称(如双括号的形式),但你不能添加各地运营空间:

 声明-i X = 1#组整数,初始化为1
声明+ I S = 1#清整,初始化为1
X + = 1#也可以是X = X + 1个
回声$ X#结果:2(加)
S + = 1#也可以是S = $ S + 1,需要一个$
回声$ S#结果:11(字符串连接)

与上面的表格,要求 EXPR 涉及产卵这对于在一个循环中大量的计算相当昂贵的外部可执行文件。它应使用的只有的时间是在环境中shell不能做自己的算术或便携当脚本可能会发现它的方式进入这样的环境。 POSIX的炮弹有运算能力因此这将是一个只关注与旧系统。

关于使用的BC 的浮点运算,它或类似的东西是用Bash和其他许多炮弹时需要。 POSIX说:需要很长整数运算只有签署。

这不支持浮点数学运算两种炮弹是KSH和zsh的。除了 BC ,您可以用 DC ,AWK,Python和Perl和其他人从Bash脚本中。

一件事,猛砸的将会的做浮点数与的printf 内置(注意,也有外部打印出来的printf ,但是内建有优先权)。

 的printf%'14 .4f \\ n#1234.56结果1,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算术防爆pressions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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