2.Shell的数值运算

一.Shell的数值运算

  1. expr 整数运算
    [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: 非整数参数
    案例:  判断传参输入是否为整数
        [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 "你输入的是非整数" || echo "你输入的是整数"
        你输入的是整数
        [root@web01 scripts]# expr $num2 + 1 >/dev/null 2>&1
        [root@web01 scripts]# [ $? -ne 0 ] && echo "你输入的是非整数" || echo "你输入的是整数"
        你输入的是非整数

2.$(()) 整数运算 运算效率最高

        [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.$[] 整数运算

        [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@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.小结:expr (())[] let bc awk python

案例1:

        ps axu 中的VSZ 列 所有的数相加 得出结果
        [root@web01 scripts]# ps axuf|awk '{print $5}'|grep -v VSZ|tr "\n" "+"|sed -r 's#(.*)#\10\n#g'|bc
        12778952

案例2:

        做一个加减乘除的计算器
        例如:
        请输入第一个数字 10
        请输入第二个数字 20
        显示的结果  10+20=30
        
        [root@web01 scripts]# cat count.sh 
        #!/bin/sh
        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]"

二.表达式

1.条件表达式

    []======test   []常用
    [ -f file ] 文件是否存在 且为普通文件 重点
    [ -e file ] 文件存在则为真
    [ -d file ] 目录存在则为真         重点
    [ -x file ] 文件有执行权限则为真
    [ -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                                                  [失败]
    函数库使用
    [root@web01 scripts]# cat ping.sh 
    #!/bin/sh
    [ -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

    -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

三.数值比较

1.数值比较

    [ 数值1 比较符 数值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.案例

统计当前磁盘的使用率 如果大于5% 则把内容写入到以日期为名称的文本中 2019-08-01.txt
如果小了 则把当前的使用率 写入 2019-08-01.txt
1.如何取出当前的使用率
  df -h|awk 'NR==2{print $(NF-1)}'
  df -h|grep /$|awk '{print $(NF-1)}'
2.条件表达式 整数的比较
3.输出结果到文本
4.调试
[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

4.案例

统计系统内存的使用率 如果大于10则 echo 使用率到 以时间命名的文件中
1.如何取出当前的使用率
  百分比=====使用的除总数乘100
2.条件表达式 整数的比较
3.输出结果到文本
4.调试
[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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容