Linux Shell Scripting 012 (Mathematical Operations)

Arithmetic operators refer to operators that can implement mathematical operations such as addition, subtraction, multiplication, and division in a program. Commonly used mathematical operators in Shell are shown below.

  • +: Adds two variables.

  • –: Subtract two variables.

  • *: Multiplies two variables.

  • /: Divide two variables.

  • **: Do exponentiation of two variables.

  • %: Modulo operation, the first variable is divided by the second variable to obtain the remainder.

  • +=: Add equals, add the second variable to itself.

  • -=: Subtract equals, subtract the second variable from the first variable.

  • *=: Multiply equals, multiply the second variable on the basis of the first variable.

  • /=: divide equals, divide the first variable by the second variable.

  • %=: modulo assignment, the first variable takes the modulo operation on the second variable, and then assigns it to the first variable.

When using these operators, you need to pay attention to the order of operations. For example, enter the following command to output the result of 1+2.

echo 1+2

The shell did not output the result 3, but 1+2. There are three ways to change the order of operations in the shell.

1. Use expr to change the order of operations. You can use echo expr 1 +2to output the result of 1+2, and use expr to indicate that the following expression is a mathematical operation. It should be noted that ` is not a single quote, but the symbol above the "Tab" key.

[root@ansible test]# echo `expr 1 + 2`
3

2. Use let to indicate mathematical operations. You can first assign the result of the operation to the variable b, and the operation command is let m=1+2. Then use echo$m to output the value of m. If there is no let, it will output 1+2.

[root@ansible test]# let m=5+4
[root@ansible test]# echo $m
9
[root@ansible test]# m=1+2
[root@ansible test]# echo $m
1+2

3. Use [ ] surface Show number learn transport Calculate Will one indivual number learn transport Calculate Write arrive In the brackets of the [] symbol, the content in the brackets will be mathematically performed first. For example, the command echo$[1+2] will output the result 3.

[root@ansible test]# echo $[5+4]
9
[root@ansible test]# b=$[5+4]
[root@ansible test]# echo $b
9
[root@ansible test]# echo $[1.5*3]         #带小数不适用
-bash: 1.5*3: 语法错误: 无效的算术运算符 (错误符号是 ".5*3"
[root@ansible test]# echo $[1.5+3]
-bash: 1.5+3: 语法错误: 无效的算术运算符 (错误符号是 ".5+3"

4. The bc command is a calculator language that supports interactive execution of arbitrary precision. Bash has built-in support for four arithmetic operations on integers, but it does not support floating-point operations. The bc command can easily perform floating-point operations. Of course, integer operations are no longer a problem.

[root@ansible test]# echo "1+2"
1+2
[root@ansible test]# echo "1+2"|bc
3
[root@ansible test]# m='1+2'
[root@ansible test]# echo $m
1+2
[root@ansible test]# echo $m|bc
3
[root@ansible test]# m=1+2
[root@ansible test]# echo $m|bc
3
[root@ansible test]# echo 'scale=2;2/3'|bc  #scale=2表示设置小数位为2位
.66
[root@ansible test]# echo 'scale=2;5/3'|bc
1.66
[root@ansible test]# echo '1.5*3'|bc
4.5
[root@ansible test]# m=1.5*3
[root@ansible test]# echo $m|bc
4.5
[root@ansible test]# echo '1.55*3.1'|bc   #默认为两位小数
4.80
[root@ansible test]# echo 'scale=5;1.55*3.1'|bc
4.805

The following is a simple script example, this example only shows the operation method, not a suitable operation method:

#!/bin/bash
echo '输入第一个数字:'
read a
echo '输入第二个数字:'
read b
echo '运算结果如下:'
expr $a + $b &> /dev/null
if [ $? -eq 0 ];then       #借此判断a、b是否全是整数
        echo "$a+$b="$[a+b]
        echo "$a-$b="$[a-b]
        echo "$a*$b="$[a*b]
        echo "$a/$b="$[a/b]
        echo "$a%$b="$[a%b]
        echo -n "$a/$b="&&awk 'BEGIN{printf "%.5f\n",'$a' / '$b'}'
else
        echo -n "$a+$b="&&echo "$a+$b"|bc
        echo -n "$a-$b="&&echo "$a-$b"|bc
        echo -n "$a*$b="&&echo "scale=5;$a*$b"|bc
        echo -n "$a/$b="&&echo "scale=5;$a/$b"|bc
fi

Refer to http://blog.csdn.net/taijianyu/article/details/6907288

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325771593&siteId=291194637