运算符
基本介绍
学习如何在shell中进行各种运算操作
基本语法
1、"$
((运算式))"或"$
[运算式]"
2、expr m + n,注意expr运算符间要有空格
3、expr m - n
4、expr *,/,% 乘,除,取余
应用实例
案例1:计算(2+3)*4的值
[root@localhost shell]# vi compute.sh
#!/bin/bash
#第一种方式$()
RESULT1=$(((2+3)*4))
echo "result1=$RESULT1"
#第二种方式[]
RESULT2=$[(2+3)*4]
echo "result2=$RESULT2"
#第三种方式,使用expr
TEMP=`expr 2 + 3`
RESULT3=`expr $TEMP \* 4`
echo "result3=$RESULT3"
[root@localhost shell]# chmod 744 compute.sh
[root@localhost shell]# ./compute.sh
result1=20
result2=20
result3=20
案例2:请求出命令行的两个参数[整数]的和
[root@localhost shell]# vi compute.sh
#求出两个参数的和
SUB=$[$1+$2]
echo "SUB=$SUB"
[root@localhost shell]# ./compute.sh 10 10
SUB=20