标签:变量 int block 输出 运算 exp host echo localhost
变量的运算(直接命令行操作即可)
##加减乘除
[root@localhost ~]# num1=10
[root@localhost ~]# num2=16
[root@localhost ~]# expr $num1 + num2
25
[root@localhost ~]# expr $num2 - num1
6
[root@localhost ~]# expr $num1 \* num2
160
[root@localhost ~]# expr $num2 \ num1 #默认不显示小数,取整
1
$符号加(())
[root@localhost ~]# num1=10
[root@localhost ~]# num2=16
[root@localhost ~]# echo $(($num1+$num2)) #加
26
[root@localhost ~]# echo $(($num1-$num2)) #减
-6
[root@localhost ~]# echo $(($num1*$num2)) #乘
160
[root@localhost ~]# echo $(($num2/$num1)) #除,默认取整数,不显示小数
1
$符号加[]
[root@localhost ~]# num1=10
[root@localhost ~]# num2=16
[root@localhost ~]# echo $[num1 + num2] #加
26
[root@localhost ~]# echo $[num1 - num2] #减
-6
[root@localhost ~]# echo $[num1 * num2] #乘
160
[root@localhost ~]# echo $[num2 / num1] #除,同上
1
let不直接输出
[root@localhost ~]# num1=10
[root@localhost ~]# num2=16
[root@localhost ~]# let sum=$num1+$num2;echo $sum #加
26
[root@localhost ~]# let sum=$num1-$num2;echo $sum #减
-6
[root@localhost ~]# let sum=$num1*$num2;echo $sum #乘
160
[root@localhost ~]# let sum=$num2/$num1;echo $sum #除
1
scale=n;定义小数精度,此外可以直接运行bc命令进行输入计算
[root@localhost ~]# num1=1.8
[root@localhost ~]# num2=2.2
[root@localhost ~]# echo "scale=3;$num1+$num2" | bc
4.0
[root@localhost ~]# echo "scale=2;$num2-$num1" | bc
.4
[root@localhost ~]# echo "scale=2;$num2/$num1" | bc
1.22
[root@localhost ~]# echo "scale=2;$num2*$num1" | bc
3.96
awk -v 带入外部变量,‘{ print num1 + num2 }‘即可
[root@localhost project]# num1=2.1
[root@localhost project]# num2=1.5
[root@localhost project]# echo | awk -v num1=$num1 -v num2=$num2 "{print num1 + num2 }"
3.6
[root@localhost project]# echo | awk -v num1=$num1 -v num2=$num2 "{print num1 - num2 }"
0.6
[root@localhost project]# echo | awk -v num1=$num1 -v num2=$num2 "{print num1 * num2 }"
3.15
[root@localhost project]# echo | awk -v num1=$num1 -v num2=$num2 "{print num1 / num2 }"
1.4
标签:变量 int block 输出 运算 exp host echo localhost
原文地址:https://blog.51cto.com/13760226/2410180