[rps-include post=6835]
[rps-include post = 6835]
We can store different type of values in the variables. But just storing values is generally not enough and we need to make some operations with them. In this tutorial we will look different type of operators which will work with variables.
我们可以在变量中存储不同类型的值。 但是,仅存储值通常是不够的,我们需要对它们进行一些操作。 在本教程中,我们将介绍可与变量一起使用的不同类型的运算符。
分配 (Assignment)
The most used operator and generally underestimated its power. Variable assignment is made with =. By the way be aware that you do not confuse assignment operator with test operator which is the same =.
最常用的操作员通常会低估其功能。 通过=进行变量赋值。 顺便提一下,您不要将赋值运算符与相同的=混淆。
$ a=123
$ echo $a
123
让运营商 (Let Operator)
In bash scripting to make mathematical calculations and use operators let expression is used. let expression makes afterward expressions mathematically calculated. Look example below
在bash脚本进行数学计算和使用运营商让表达式中使用。 let表达式可以数学计算出随后的表达式。 看下面的例子
$ let "z=5"
$ echo $z
5
加 (Plus)
To make sum operations + is used as expected but be aware of that let should be used.
要进行总和运算+可以按预期方式使用,但请注意应使用let。
$ let "a=1"
$ let "b=1"
$ let "c=a+b"
$ echo $c
2
加等于 (Plus Equal)
Plus equal is similar to plus but makes operations easy when one of the sum parameters are same for the result
加等于等于加,但当总和参数之一与结果相同时使操作变得容易
$ var=12
$ let "var +=5"
$ echo $var
17
减去 (Minus)
Usage usage of the minus operator is the same with plus operator. As you know just the effect is different
减号运算符的用法用法与加号运算符相同。 如您所知,效果是不同的
$ let "a=1"
$ let "b=1"
$ let "c=a-b"
$ echo $c
0
负等于 (Minus Equal)
Plus equal is similar to plus but makes operations easy when one of the sum parameters are same for the result
加等于等于加,但当总和参数之一与结果相同时使操作变得容易
$ var=12
$ let "var -=5"
$ echo $var
7
乘法 (Multiplication)
You can find the multiplication operation below
您可以在下面找到乘法运算
$ let "a=1"
$ let "b=2"
$ let "c=a*b"
$ echo $c
2
Simple, isn’t is?
很简单,不是吗?
师 (Division)
You can find the division operation below
您可以在下面找到除法运算
$ let "a=4"
$ let "b=2"
$ let "c=a/b"
$ echo $c
2
求幂 (Exponentiation)
Generally this type of operations are not required in bash scripting but to be aware of this operator we make simple example
通常,bash脚本中不需要这种类型的操作,但是要知道这个操作符,我们举一个简单的例子
$ let "z=5**3"
$ echo $z
125
模数 (Modulo)
To get remainder of a division module is used. For example what will be the remainder after 5/3. We can use following code for this operation. As you will see below we have new keyword named expr
为了获得除法模块的其余部分。 例如5/3之后的余数。 我们可以使用以下代码进行此操作。 正如您将在下面看到的,我们有一个名为expr的新关键字。
$ expr 5 % 3
2
按位 (Bit-wise)
Decimal numbers can be expressed in binary format. So if we need to shift the binary values of a decimal Bit-wise operator can be used.
十进制数可以二进制格式表示。 因此,如果需要移位十进制的二进制值,可以使用按位运算符。
$ var=2
$ let "var<<=2"
$ echo $var
8
In this example we have rotated binary for 2 times. Before the rotation binary value was like 0000010. After the rotation it is like 0001000 where its decimal value is 8. We have used = to make assignment easy.
在此示例中,我们将二进制文件旋转了2次。 旋转前的二进制值类似于0000010。旋转后的二进制值类似于0001000,其十进制值为8。我们使用=来简化赋值。
Bit-wise operations can be done in reverse order like >>.
可以按相反的顺序执行按位运算,例如>> 。
和 (And)
Logical operations are important for a programming language. Bash have full support for logical operations like AND.
逻辑运算对于编程语言很重要。 Bash完全支持AND等逻辑运算。
[rps-include post=6835]
[rps-include post = 6835]
翻译自: https://www.poftut.com/linux-bash-operators-like-assignment-plus-calculation/