2.Shell的數(shù)值運算

一.Shell的數(shù)值運算

  1. 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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末描滔,一起剝皮案震驚了整個濱河市蚊俺,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖涩哟,帶你破解...
    沈念sama閱讀 211,948評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異驹马,居然都是意外死亡卷拘,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,371評論 3 385
  • 文/潘曉璐 我一進店門氮墨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來纺蛆,“玉大人,你說我怎么就攤上這事规揪∏攀希” “怎么了?”我有些...
    開封第一講書人閱讀 157,490評論 0 348
  • 文/不壞的土叔 我叫張陵猛铅,是天一觀的道長字支。 經(jīng)常有香客問我,道長奸忽,這世上最難降的妖魔是什么堕伪? 我笑而不...
    開封第一講書人閱讀 56,521評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮栗菜,結(jié)果婚禮上欠雌,老公的妹妹穿的比我還像新娘。我一直安慰自己疙筹,他們只是感情好富俄,可當(dāng)我...
    茶點故事閱讀 65,627評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著腌歉,像睡著了一般蛙酪。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上翘盖,一...
    開封第一講書人閱讀 49,842評論 1 290
  • 那天桂塞,我揣著相機與錄音,去河邊找鬼馍驯。 笑死阁危,一個胖子當(dāng)著我的面吹牛玛痊,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播狂打,決...
    沈念sama閱讀 38,997評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼擂煞,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了趴乡?” 一聲冷哼從身側(cè)響起对省,我...
    開封第一講書人閱讀 37,741評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎晾捏,沒想到半個月后蒿涎,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,203評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡惦辛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,534評論 2 327
  • 正文 我和宋清朗相戀三年劳秋,在試婚紗的時候發(fā)現(xiàn)自己被綠了斋否。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片梗劫。...
    茶點故事閱讀 38,673評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖礁扮,靈堂內(nèi)的尸體忽然破棺而出呀伙,到底是詐尸還是另有隱情补履,我是刑警寧澤,帶...
    沈念sama閱讀 34,339評論 4 330
  • 正文 年R本政府宣布区匠,位于F島的核電站干像,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏驰弄。R本人自食惡果不足惜麻汰,卻給世界環(huán)境...
    茶點故事閱讀 39,955評論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望戚篙。 院中可真熱鬧五鲫,春花似錦、人聲如沸岔擂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,770評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽乱灵。三九已至塑崖,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間痛倚,已是汗流浹背规婆。 一陣腳步聲響...
    開封第一講書人閱讀 32,000評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人抒蚜。 一個月前我還...
    沈念sama閱讀 46,394評論 2 360
  • 正文 我出身青樓掘鄙,卻偏偏與公主長得像,于是被迫代替她去往敵國和親嗡髓。 傳聞我的和親對象是個殘疾皇子操漠,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,562評論 2 349

推薦閱讀更多精彩內(nèi)容