一.Shell的數(shù)值運算
- expr 整數(shù)運算
[root@web01 scripts]# expr 1+1
1+1
[root@web01 scripts]# expr oldboy
oldboy
[root@web01 scripts]# expr 1 + 1
2
[root@web01 scripts]# expr 1 - 1
0
[root@web01 scripts]# expr 1 * 11
expr: 語法錯誤
[root@web01 scripts]# expr 1 \* 11
11
[root@web01 scripts]# expr 110 / 11
10
[root@web01 scripts]# expr 1 + 1.5
expr: 非整數(shù)參數(shù)
案例: 判斷傳參輸入是否為整數(shù)
[root@web01 scripts]# num1=10
[root@web01 scripts]# num2=11q
[root@web01 scripts]# expr $num1 + 1 >/dev/null 2>&1
[root@web01 scripts]# [ $? -ne 0 ] && echo "你輸入的是非整數(shù)" || echo "你輸入的是整數(shù)"
你輸入的是整數(shù)
[root@web01 scripts]# expr $num2 + 1 >/dev/null 2>&1
[root@web01 scripts]# [ $? -ne 0 ] && echo "你輸入的是非整數(shù)" || echo "你輸入的是整數(shù)"
你輸入的是非整數(shù)
2.$(()) 整數(shù)運算 運算效率最高
[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
3.$[] 整數(shù)運算
[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: 語法錯誤: 無效的算術(shù)運算符 (錯誤符號是 ".5")
4.let 重點
[root@web01 scripts]# let sum=10+10
[root@web01 scripts]# echo $sum
20
[root@web01 scripts]# num1=100
[root@web01 scripts]# num2=50
[root@web01 scripts]# let sum=$num1+$num2
[root@web01 scripts]# echo $sum
150
[root@web01 scripts]# let sum=$num1-$num2
[root@web01 scripts]# echo $sum
50
[root@web01 scripts]# let sum=$num1*$num2
[root@web01 scripts]# echo $sum
5000
[root@web01 scripts]# let sum=$num1/$num2
[root@web01 scripts]# echo $sum
2
i++ 變量自增
[root@web01 scripts]# let i=i+1
[root@web01 scripts]# echo $i
1
[root@web01 scripts]# let i++
[root@web01 scripts]# echo $i
2
[root@web01 scripts]# let i++
[root@web01 scripts]# let i++
[root@web01 scripts]# let i++
[root@web01 scripts]# let i++
[root@web01 scripts]# echo $i
6
[root@web01 scripts]# sh for.sh
7
[root@web01 scripts]# sh -x for.sh
+ for i in I am lizhenya teacher I am 18
+ let a++
+ for i in I am lizhenya teacher I am 18
+ let a++
+ for i in I am lizhenya teacher I am 18
+ let a++
+ for i in I am lizhenya teacher I am 18
+ let a++
+ for i in I am lizhenya teacher I am 18
+ let a++
+ for i in I am lizhenya teacher I am 18
+ let a++
+ for i in I am lizhenya teacher I am 18
+ let a++
+ echo 7
7
[root@web01 scripts]# let c++
[root@web01 scripts]# let c++
[root@web01 scripts]# let c++
[root@web01 scripts]# let c++
[root@web01 scripts]# let c++
[root@web01 scripts]# let c++
[root@web01 scripts]# let c++
[root@web01 scripts]# echo $c
7
5.bc awk python (bc需要安裝)
[root@web01 scripts]# echo 10+10|bc
20
[root@web01 scripts]# echo 10-10|bc
0
[root@web01 scripts]# echo 10*10|bc
100
[root@web01 scripts]# echo 10/10|bc
1
[root@web01 scripts]# echo 10+10.5|bc
20.5
[root@web01 scripts]# awk 'BEGIN{print 10+10}'
20
[root@web01 scripts]# awk 'BEGIN{print 10-10}'
0
[root@web01 scripts]# awk 'BEGIN{print 10/10.5}'
0.952381
[root@web01 scripts]# awk 'BEGIN{print 10*10.5}'
105
[root@web01 scripts]# awk 'BEGIN{print 10^10.5}'
3.16228e+10
[root@web01 scripts]# awk 'BEGIN{print 10^10}'
10000000000
[root@web01 scripts]# echo 100 200
100 200
[root@web01 scripts]# echo 100 200|awk '{print $1+$2}'
300
[root@web01 scripts]# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 10+10
20
>>> 10-10
0
>>> 10*10
100
>>> 10/10
1
>>> 10/10.5
0.9523809523809523
>>>
6.小結(jié):expr [] let bc awk python
案例1:
ps axu 中的VSZ 列 所有的數(shù)相加 得出結(jié)果
[root@web01 scripts]# ps axuf|awk '{print $5}'|grep -v VSZ|tr "\n" "+"|sed -r 's#(.*)#\10\n#g'|bc
12778952
案例2:
做一個加減乘除的計算器
例如:
請輸入第一個數(shù)字 10
請輸入第二個數(shù)字 20
顯示的結(jié)果 10+20=30
[root@web01 scripts]# cat count.sh
#!/bin/sh
read -p "請輸入第一個數(shù)字: " num1
read -p "請輸入第二個數(shù)字: " num2
echo "$num1+$num2=$[$num1+$num2]"
echo "$num1-$num2=$[$num1-$num2]"
echo "$num1*$num2=$[$num1*$num2]"
echo "$num1/$num2=$[$num1/$num2]"
二.表達式
1.條件表達式
[]======test []常用
[ -f file ] 文件是否存在 且為普通文件 重點
[ -e file ] 文件存在則為真
[ -d file ] 目錄存在則為真 重點
[ -x file ] 文件有執(zhí)行權(quán)限則為真
[ -w file ] 文件可寫則為真
[ -r file ] 文件可讀則為真
舉例:
[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]# [ -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 [失敗]
函數(shù)庫使用
[root@web01 scripts]# cat ping.sh
#!/bin/sh
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
read -p "請輸入一個網(wǎng)址: " 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
-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
三.數(shù)值比較
1.數(shù)值比較
[ 數(shù)值1 比較符 數(shù)值2 ]
-eq 相等
-ne 不等于
-gt 大于
-ge 大于等于
-lt 小于
-le 小于等于
2.舉例
[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
3.案例
統(tǒng)計當(dāng)前磁盤的使用率 如果大于5% 則把內(nèi)容寫入到以日期為名稱的文本中 2019-08-01.txt
如果小了 則把當(dāng)前的使用率 寫入 2019-08-01.txt
1.如何取出當(dāng)前的使用率
df -h|awk 'NR==2{print $(NF-1)}'
df -h|grep /$|awk '{print $(NF-1)}'
2.條件表達式 整數(shù)的比較
3.輸出結(jié)果到文本
4.調(diào)試
[root@shell scripts]# cat usedisk.sh
#!/bin/sh
time=`date +%F`
usedisk=`df -h|grep /$|awk '{print $(NF-1)}'`
[ ${usedisk%\%} -gt 5 ] && echo "當(dāng)前使用率不正常 $usedisk" >> ${time}.txt || echo "當(dāng)前使用率正常 $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
4.案例
統(tǒng)計系統(tǒng)內(nèi)存的使用率 如果大于10則 echo 使用率到 以時間命名的文件中
1.如何取出當(dāng)前的使用率
百分比=====使用的除總數(shù)乘100
2.條件表達式 整數(shù)的比較
3.輸出結(jié)果到文本
4.調(diào)試
[root@web01 scripts]# free|awk 'NR==2{print $3/$2*100}'
19.0124
[root@web01 scripts]# free=`free|awk 'NR==2{print $3/$2*100}'`
[root@web01 scripts]# [ ${free%.*} -gt 10 ] && echo error || echo ok
error
[root@web01 scripts]# [ ${free%.*} -gt 80 ] && echo error || echo ok
ok