bash脚本编程之算术运算
+,-,*,/, **, %
算术运算格式:
- let VAR=EXPRESSION
- VAR=$[EXPRESSION]
- VAR=$((EXPRESSION))
- VAR=$(expr $ARG1 $OP $ARG2)
man expr
注意:乘法符号在有些场景中需要使用转义符;
练习
写一个脚本,完成如下功能:
- 添加三个用户;
- 求此三个用户的UID之和;
[root@localhost ~]# bash /tmp/note7_3.sh
uid=4013(shtest1) gid=4013(shtest1) groups=4013(shtest1)
uid=4014(shtest2) gid=4014(shtest2) groups=4014(shtest2)
uid=4015(shtest3) gid=4015(shtest3) groups=4015(shtest3)
12042
[root@localhost ~]# cat /tmp/note7_3.sh
#!/bin/bash
uname1=shtest1 && uname2=shtest2 && uname3=shtest3
id $uname1 || useradd $uname1
id $uname2 || useradd $uname2
id $uname3 || useradd $uname3
uid1=`grep $uname1 /etc/passwd | cut -d : -f3`
uid2=`grep $uname2 /etc/passwd | cut -d : -f3`
uid3=`grep $uname3 /etc/passwd | cut -d : -f3`
echo $[$uid1+$uid2+$uid3]