算术运算格式:
(1) let VAR=算术运算表达式
[[email protected] tmp]# sum=1 [[email protected] tmp]# echo $sum 1 [[email protected] tmp]# echo $sum1 501 [[email protected] tmp]# echo $sum2 495 [[email protected] tmp]# let sum=sum1+sum2 [[email protected] tmp]# echo $sum 996
(2) VAR=$[算术运算表达式]
sum=$[`id -u dts`+`id -u svn`+`id -u centos`] echo $sum
(3) VAR=$((算术运算表达式))
sum=$((`id -u dts`+`id -u svn`+`id -u centos`)) echo $sum
(4) VAR=$(expr $ARG1 $OP $ARG2)
sum1=`id -u dts` sum2=`id -u svn` sum=$(expr $sum1 + $sum2) echo $sum sum=$(expr $sum1 \* $sum2) echo $sum
注意:乘法符号在有些场景中需要使用转义符;
练习:写一个脚本,完成如下功能:
添加三个用户;
求此三个用户的UID之和;
#!/bin/bash sum=$[`id -u dts`+`id -u svn`+`id -u centos`] echo $sum sum=$((`id -u dts`+`id -u svn`+`id -u centos`)) echo $sum sum1=`id -u dts` sum2=`id -u svn` sum=$(expr $sum1 + $sum2) echo $sum sum=$(expr $sum1 \* $sum2) echo $sum
时间: 2024-11-22 16:40:10