数值与条件表达式
数值与条件表达式
数值运算
1.加减乘除,expr 整数运算
数值前后都要加空格
不支持小数
[root@shell /server/scripts]# expr 1 + 1
2
[root@shell /server/scripts]# expr 1 - 1
0
[root@shell /server/scripts]# expr 1 \* 11 *号在这里是通配符的意思,所以要转义
11
[root@shell /server/scripts]# expr 12 / 1
12
[root@web01 scripts]# expr 1 + 1.5
expr: 非整数参数
案例: 判断传参输入是否为整数?
[root@shell /server/scripts]# num1=10
[root@shell /server/scripts]# num2=11q
[root@shell /server/scripts]# expr $num1 + 1 >/dev/null 2>&1
[root@shell /server/scripts]# [ $? -ne 0 ] && echo "你输入的 是非整数" || echo "好的,是整数"
好的,是整数
[root@shell /server/scripts]# expr $num2 + 1 >/dev/null 2>&1
[root@shell /server/scripts]# [ $? -ne 0 ] && echo "你输入的 是非整数" || echo "好的,是整数"
你输入的是非整数
2. $(()) 整数运算 (运算效率最高)
不支持小数
[root@shell /server/scripts]# echo $((10+10))
20
[root@shell /server/scripts]# echo $((10-10))
0
[root@shell /server/scripts]# echo $((10*10)) 不需要转义
100
[root@shell /server/scripts]# echo $((10/10))
1
3. $[] 整数运算
不支持小数
[root@web01 scripts]# echo $[10+10]
20
[root@web01 scripts]# echo $[10-10]
0
[root@web01 scripts]# echo $[10*10]
100
[root@web01 scripts]# echo $[10/10]
1
[root@web01 scripts]# echo $[10-10.5]
-bash: 10-10.5: 语法错误: 无效的算术运算符 (错误符号是 ".5")
4.let 重点
不支持小数
#这是格式
[root@shell /server/scripts]# let sum=10+10
[root@shell /server/scripts]# echo $sum
20
[root@shell /server/scripts]# num1=99
[root@shell /server/scripts]# num2=999
[root@shell /server/scripts]# let sum=$num1+$num2
[root@shell /server/scripts]# echo $sum
1098
[root@shell /server/scripts]# let sum=$num1-$num2
[root@shell /server/scripts]# echo $sum 答案是负数好像可以
-900
[root@shell /server/scripts]# let sum=$num1*$num2
[root@shell /server/scripts]# echo $sum
98901
[root@shell /server/scripts]# let sum=$num1/$num2 小数除大数 就不行
[root@shell /server/scripts]# echo $sum
0
[root@shell /server/scripts]# let sum=$num2/$num1
[root@shell /server/scripts]# echo $sum
10
变量自增
[root@shell /server/scripts]# let i++ i++的意思是i=i+1
[root@shell /server/scripts]# echo $i
1
[root@shell /server/scripts]# let i++ 这个时候i就等于1+1=2了
[root@shell /server/scripts]# echo $i
2
[root@shell /server/scripts]# let i++
[root@shell /server/scripts]# let i++
[root@shell /server/scripts]# let i++
[root@shell /server/scripts]# let i++
[root@shell /server/scripts]# echo $i
6
[root@shell /server/scripts]# cat for.sh
#!/bin/bash
##############################################################
# File Name: for.sh
# Time: 2019-10-31-09:10:33
# Author: msy
##############################################################
for i in I am mengshiyu I am 17
do
let b++
done
echo $b
[root@shell /server/scripts]# sh for.sh 因为我循环了6次 (i am mengshiyu i am 17) 一共6位
6
[root@shell /server/scripts]# sh -x for.sh
+ for i in I am mengshiyu I am 17
+ let b++
+ for i in I am mengshiyu I am 17
+ let b++
+ for i in I am mengshiyu I am 17
+ let b++
+ for i in I am mengshiyu I am 17
+ let b++
+ for i in I am mengshiyu I am 17
+ let b++
+ for i in I am mengshiyu I am 17
+ let b++
+ echo 6
6
5.bc awk python (bc需安装)
bc
[root@shell /server/scripts]# echo 10+10|bc
20
[root@shell /server/scripts]# echo 10*10|bc
100
[root@shell /server/scripts]# echo 10/10|bc
1
[root@shell /server/scripts]# echo 10-10|bc
0
[root@shell /server/scripts]# echo 10+10.5|bc 小数支持了!!!
20.5
[root@shell /server/scripts]# echo 10/10.5|bc 这样除还是不支持!!
0
awk
[root@shell /server/scripts]# awk 'BEGIN{print 10+10}'
20
[root@shell /server/scripts]# awk 'BEGIN{print 10*10}'
100
[root@shell /server/scripts]# awk 'BEGIN{print 10-10}'
0
[root@shell /server/scripts]# awk 'BEGIN{print 10/10}'
1
[root@shell /server/scripts]# awk 'BEGIN{print 10/10.5}' 支持
0.952381
[root@shell /server/scripts]# awk 'BEGIN{print 10-10.5}' 支持
-0.5
[root@shell /server/scripts]# awk 'BEGIN{print 10*10.5}'
105
[root@shell /server/scripts]# awk 'BEGIN{print 10+10.5}' 支持
20.5
[root@shell /server/scripts]# awk 'BEGIN{print 10+10.5*10}'
115
[root@web01 scripts]# echo 100 200
100 200
[root@web01 scripts]# echo 100 200|awk '{print $1+$2}'
300
python
[root@shell /server/scripts]# python
Python 2.7.5 (default, Apr 11 2018, 07:36:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 10+10
20
>>> 10-10
0
>>> 10/10
1
>>> 10*10
100
>>> 10+10.5
20.5
>>> 10-10.5
-0.5
>>> 10/10.5
0.9523809523809523
>>> 10*10.5
105.0
>>> 10+10.5*10
115.0
Ctrl-D
案例1:ps axu 中的VSZ 列 所有的数相加 得出结果
[root@shell /server/scripts]# ps axu |grep VSZ 找到是第五列!!
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1783 0.0 0.0 112704 972 pts/0 R+ 09:32 0:00 grep --color=auto VSZ
[root@shell /server/scripts]# ps axuf|awk '{print $5}'|grep -v VSZ|tr "\n" "+"|sed -r 's#(.*)#\10\n#g'|bc 后向引用 将前面所有得加0加回车
4127724
案例2:做一个加减乘除的计算器
例如:
请输入第一个数字 10
请输入第二个数字 20
显示的结果 10+20=30
[root@shell /server/scripts]# vim jsq.sh
#!/bin/bash
#############################################################
# File Name: jsq.sh
# Time: 2019-10-31-09:50:13
# Author: msy
#############################################################
read -p "请输入第一个数字:" num1
read -p "请输入第二个数字:" num2
echo "$num1+$num2=$[$num1+$num2]"
echo "$num1-$num2=$[$num1-$num2]"
echo "$num1*$num2=$[$num1*$num2]"
echo "$num1/$num2=$[$num1/$num2]"
[root@shell /server/scripts]# sh jsq.sh
请输入第一个数字:99
请输入第二个数字:20
99+20=119
99-20=79
99*20=1980
99/20=4 `这个除法只能整除!!!
[root@shell /server/scripts]# sh jsq.sh
请输入第一个数字:100
请输入第二个数字:20
100+20=120
100-20=80
100*20=2000
100/20=5
条件表达式
文件的条件表达式
文件属性判断
演算子 | 内容 |
---|---|
[-f 文件名] | 普通文件且存在的话 真 |
[-d 文件名] | 路径目录存在的话 真 |
[-e 文件名] | 文件存在的话 真 |
[-L 文件名] | 符号链接的话 真 |
[-r 文件名] | 可读文件的话 真 |
[-w 文件名] | 可写入文件的话 真 |
[-x 文件名] | 文件存在且可以执行的话 真 |
[-s 文件名] | 文件大小不为0的话 真 |
[f1 -nt f2] | 文件f1比文件f2新的话 真 |
[f1 -ot f2] | 文件f1比文件f2旧的话 真 |
[root@web01 scripts]# [ -f /etc/hosts ] && echo 为真 || echo 为假
为真
[root@web01 scripts]# [ -f /etc/hostsss ] && echo 为真 || echo 为假
为假
[root@web01 scripts]# [ -d /etc/hosts ] && echo 为真 || echo 为假
为假
[root@web01 scripts]# [ ! -d /etc/hosts ] && echo 为真 || echo 为假 (取反)
为真
[root@web01 scripts]# [ -e /etc/hosts ] && echo 为真 || echo 为假
为真
[root@web01 scripts]# [ -e /etc/ ] && echo 为真 || echo 为假
为真
[root@web01 scripts]# [ -r /etc/ ] && echo 为真 || echo 为假
为真
[root@web01 scripts]# [ -w /etc/ ] && echo 为真 || echo 为假
为真
[root@web01 scripts]# [ -x /etc/ ] && echo 为真 || echo 为假
为真
[root@web01 scripts]# [ -x /etc/hosts ] && echo 为真 || echo 为假
为假
[root@web01 scripts]# [ -x /etc/hosts ] && echo 为真 || echo 为假
为假
[root@web01 scripts]# ll
总用量 24
-rw-r--r-- 1 root root 230 8月 1 16:07 count.sh
-rw-r--r-- 1 root root 74 8月 1 15:33 for.sh
-rw-r--r-- 1 root root 297 7月 31 16:59 ip.sh
-rw-r--r-- 1 root root 14 8月 1 15:06 oldboy.sh
-rw-r--r-- 1 root root 152 7月 31 17:12 ping.sh
-rwxr-xr-x 1 root root 225 7月 31 16:34 test.sh
[root@web01 scripts]# [ -x test.sh ] && echo 为真 || echo 为假
为真
案例:
-f 判断文件是否存在
`判断是否存在 如果存在请调用
[root@web01 scripts]# [ -f /etc/init.d/functions ] && . /etc/init.d/functions(函数库)
[root@web01 scripts]# action "hehehe is" /bin/true
hehehe is [ 确定 ]
[root@web01 scripts]# action "hehehe is" /bin/false
hehehe is [失败]
`函数库的脚本玩法
[root@shell /server/scripts]# vim ping.sh
#!/bin/bash
#############################################################
# File Name: ping.sh
# Time: 2019-10-31-10:18:20
# Author: msy
#############################################################
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
read -p "请输入一个网址:" url
ping -c 1 -W 1 $url >/dev/null 2>&1
[ $? -eq 0 ] && action "ping $url is" /bin/true || action "ping $url is" /bin/false
[root@shell /server/scripts]# sh ping.sh
请输入一个网址:www.baidu.com
ping www.baidu.com is [ OK ]
[root@shell /server/scripts]# sh ping.sh
请输入一个网址:msy.oldboy.com
ping msy.oldboy.com is [FAILED]
-d 判断是否为目录 目录是否存在
[root@web01 scripts]# [ -d /alex ] || mkdir /alex 目录存在吗?不存在则创建
[root@web01 scripts]# test -f /etc/hosts && echo ok || echo error
ok
[root@web01 scripts]# test -d /etc/hosts && echo ok || echo error
error
[root@web01 scripts]# test -d /etc/ && echo ok || echo error
ok
数值比较
数字比较
比较公式 | 比较内容 |
---|---|
a -eq b | 相等的话 真 |
a -ne b | 不相等的话 真 |
a -ge b | a >= b 真 |
a -le b | a =<b 真 |
a -gt b | a > b 真 |
a -lt b | a <b 真 |
工作场景:推荐[]的-eq的用法。!!!!
[root@web01 scripts]# [ 10 -eq 10 ] && echo ok || echo error
ok
[root@web01 scripts]# [ 10 -gt 10 ] && echo ok || echo error
error
[root@web01 scripts]# [ 10 -ge 10 ] && echo ok || echo error
ok
[root@web01 scripts]# [ 10 -ne 10 ] && echo ok || echo error
error
[root@web01 scripts]# [ 10 -lt 10 ] && echo ok || echo error
error
[root@web01 scripts]# [ 10 -le 10 ] && echo ok || echo error
ok
案例1:统计当前磁盘的使用率 如果大于5% 则把内容写入到以日期为名称的文本中 xxxx-xx-xx.txt
如果小了 则把当前的使用率 写入 xxxx-xx-xx.txt
`取出当前的使用率
df -h|awk 'NR==2{print $(NF-1)}'
df -h|grep /$|awk '{print $(NF-1)}'
[root@shell scripts]# cat usedisk.sh
#!/bin/sh
time=`date +%F`
usedisk=`df -h|grep /$|awk '{print $(NF-1)}'`
[ ${usedisk%\%} -gt 5 ] && echo "当前使用率不正常 $usedisk" >> ${time}.txt || echo "当前使用率正常 $usedisk" >> ${time}.txt
[root@web01 scripts]# [ `df -h|grep /$|awk -F "[ %]+" '{print $(NF-1)}'` -gt 5 ] && echo error || echo ok
error
[root@web01 scripts]# [ `df -h|grep /$|awk -F "[ %]+" '{print $(NF-1)}'` -gt 80 ] && echo error || echo ok
ok
案例2:查看内存当前使用状态,如果使用率超过80%则报警发邮件
[root@shell ~]# yum install mailx -y 下载邮件配置
[root@shell ~]# vim /etc/mail.rc
set from=912418275@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=912418275@qq.com
set smtp-auth-password=qtqwcqgtoyqhbfga 授权码
set smtp-auth=login
[root@shell /server/scripts]# vim free.sh
#!/bin/bash
##############################################################
# File Name: free.sh
# Time: 2019-10-31-10:49:03
# Author: msy
##############################################################
free=`free|awk 'NR==2{print $3/$2*100}'`
if [ ${free%.*} -gt 80 ]
then
echo "内存使用率超过80%" |mailx -s "报警!" 912418275@qq.com
else
echo "内存使用率正常"
fi
[root@shell /server/scripts]# sh free.sh