(四)shell编程之条件测试

条件测试:判断某需求是否满足,需要由测试机制来实现,专用的测试表达式需要由测试命令辅助完成

测试过程,实现评估布尔声明,以便用在条件性环境下进行执行

若真,则状态码变量 $? 返回0
若假,则状态码变量 $? 返回1

条件测试命令

test EXPRESSION
[ EXPRESSION ] #和test 等价,建议使用 [ ]
[[ EXPRESSION ]] 相关于增强版的 [ ], 支持[]的用法,且支持扩展正则表达式和通配符

注意:EXPRESSION前后必须有空白字符

帮助:

[root@localhost ~]# type [
[ is a shell builtin
[root@localhost ~]# help [
[: [ arg... ]
    Evaluate conditional expression.
    
    This is a synonym for the "test" builtin, but the last argument must
    be a literal `]', to match the opening `['.
[root@localhost ~]# help test
test: test [expr]
    Evaluate conditional expression.
    
    Exits with a status of 0 (true) or 1 (false) depending on
    the evaluation of EXPR.  Expressions may be unary or binary.  Unary
    expressions are often used to examine the status of a file.  There
    are string operators and numeric comparison operators as well.
    
    The behavior of test depends on the number of arguments.  Read the
    bash manual page for the complete specification.
    
    File operators:
    
      -a FILE        True if file exists.
      -b FILE        True if file is block special.
      -c FILE        True if file is character special.
      -d FILE        True if file is a directory.
      -e FILE        True if file exists.
      -f FILE        True if file exists and is a regular file.
      -g FILE        True if file is set-group-id.
      -h FILE        True if file is a symbolic link.
      -L FILE        True if file is a symbolic link.
      -k FILE        True if file has its `sticky' bit set.
      -p FILE        True if file is a named pipe.
      -r FILE        True if file is readable by you.
      -s FILE        True if file exists and is not empty.
      -S FILE        True if file is a socket.
      -t FD          True if FD is opened on a terminal.
      -u FILE        True if the file is set-user-id.
      -w FILE        True if the file is writable by you.
      -x FILE        True if the file is executable by you.
      -O FILE        True if the file is effectively owned by you.
      -G FILE        True if the file is effectively owned by your group.
      -N FILE        True if the file has been modified since it was last read.
    
      FILE1 -nt FILE2  True if file1 is newer than file2 (according to
                       modification date).
    
      FILE1 -ot FILE2  True if file1 is older than file2.
    
      FILE1 -ef FILE2  True if file1 is a hard link to file2.
    
    String operators:
    
      -z STRING      True if string is empty.
    
      -n STRING
         STRING      True if string is not empty.
    
      STRING1 = STRING2
                     True if the strings are equal.
      STRING1 != STRING2
                     True if the strings are not equal.
      STRING1 < STRING2
                     True if STRING1 sorts before STRING2 lexicographically.
      STRING1 > STRING2
                     True if STRING1 sorts after STRING2 lexicographically.
    
    Other operators:
    
      -o OPTION      True if the shell option OPTION is enabled.
      -v VAR         True if the shell variable VAR is set.
      -R VAR         True if the shell variable VAR is set and is a name
                     reference.
      ! EXPR         True if expr is false.
      EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
      EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
    
      arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                     -lt, -le, -gt, or -ge.
    
    Arithmetic binary operators return true if ARG1 is equal, not-equal,
    less-than, less-than-or-equal, greater-than, or greater-than-or-equal
    than ARG2.
    
    Exit Status:
    Returns success if EXPR evaluates to true; fails if EXPR evaluates to
    false or an invalid argument is given.

变量测试

#判断 NAME 变量是否定义
[ -v NAME ]
#判断 NAME 变量是否定义并且是名称引用,bash 4.4新特性
[ -R NAME ]

范例:

[root@localhost ~]# unset x
[root@localhost ~]# test -v x
[root@localhost ~]# echo $?
1

[root@localhost ~]# x=10
[root@localhost ~]# test -v x
[root@localhost ~]# echo $?
0

[root@localhost ~]# y=
[root@localhost ~]# test -v y
[root@localhost ~]# echo $?
0

#注意 [ ] 需要空格,否则会报下面错误
[root@localhost ~]# [-v y]
-bash: [-v: command not found
[root@localhost ~]# [ -v y ]
[root@localhost ~]# echo $?
0

数值测试

-eq 是否等于
-ne 是否不等于
-gt 是否大于
-ge 是否大于等于
-lt 是否小于
-le 是否小于等于

范例:

[root@localhost ~]# i=10
[root@localhost ~]# j=8
[root@localhost ~]# [ $ -lt $j ]
-bash: [: $: integer expression expected
[root@localhost ~]# [ $i -lt $j ]
[root@localhost ~]# echo $?
1

[root@localhost ~]# [ $i -gt $j ]
[root@localhost ~]# echo $?
0

[root@localhost ~]# [ i -gt j ]
-bash: [: i: integer expression expected

算术表达式比较

== 相等
!= 不相等
<=
>=
<
>

范例:

[root@localhost ~]# x=10;y=10;((x==y));echo $?
0
[root@localhost ~]# x=10;y=20;((x==y));echo $?
1
[root@localhost ~]# x=10;y=20;((x!=y));echo $?
0
[root@localhost ~]# x=10;y=10;((x!=y));echo $?
1

范例:

[root@localhost ~]# x=10;y=20;((x>y));echo $?
1
[root@localhost ~]# x=10;y=20;((x<y));echo $?
0

字符串测试

test和 [ ]用法

test和 [ ]用法
-z STRING 字符串是否为空,没定义或空为真,不空为假,
-n STRING 字符串是否不空,不空为真,空为假
STRING 同上
STRING1 = STRING2 是否等于,注意 = 前后有空格
STRING1 != STRING2 是否不等于
> ascii码是否大于ascii码
< 是否小于

[[]] 用法

[[ expression ]] 用法
== 左侧字符串是否和右侧的PATTERN相同
注意:此表达式用于[[ ]]中,PATTERN为通配符
=~ 左侧字符串是否能够被右侧的正则表达式的PATTERN所匹配
注意: 此表达式用于[[ ]]中;扩展的正则表达式

建议:当使用正则表达式或通配符使用[[ ]],其它情况一般使用 [ ]

范例:使用 [ ]

[root@localhost ~]# unset str
[root@localhost ~]# [ -z "$str" ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# str=""
[root@localhost ~]# [ -z "$str" ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# str=" "
[root@localhost ~]# [ -z "$str" ]
[root@localhost ~]# echo $?
1
[root@localhost ~]# [ -n "$str" ] 
[root@localhost ~]# echo $?
0
[root@localhost ~]# [ "$str" ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# str=longwang
[root@localhost ~]# [ "$str" ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# str1=longwang
[root@localhost ~]# str2=longge
[root@localhost ~]# [ $str1 = $str2 ]
[root@localhost ~]# echo $?
1
[root@localhost ~]# str2=longwang
[root@localhost ~]# [ $str1 = $str2 ]
[root@localhost ~]# echo $?
0

范例:在比较字符串时,建议变量放在“ ”中

[root@localhost ~]# [ "$NAME" ]
[root@localhost ~]# NAME="I love you"
[root@localhost ~]# [ $NAME ]
-bash: [: love: binary operator expected
[root@localhost ~]# [ "$NAME" ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# [ I love you ]
-bash: [: love: binary operator expected

范例: [[ ]] 和通配符

[root@localhost ~]# FILE="a*"
[root@localhost ~]# echo $FILE 
anaconda-ks.cfg
[root@localhost ~]# [[ $FILE == a* ]]
[root@localhost ~]# echo $?
0
[root@localhost ~]# FILE="ab"
[root@localhost ~]# [[ $FILE == a* ]]
[root@localhost ~]# echo $?
0
#[[]]中如果不想使用通配符*,只想表达*本身,可以用" "引起来
[root@localhost ~]# FILE="a*"
[root@localhost ~]# echo $FILE 
anaconda-ks.cfg
[root@localhost ~]# [[ $FILE == a* ]]
[root@localhost ~]# echo $?
0
[root@localhost ~]# FILE="ab"
[root@localhost ~]# [[ $FILE == a* ]]
[root@localhost ~]# echo $?
0
[root@localhost ~]# FILE="a*"
[root@localhost ~]# 
[root@localhost ~]# [[ $FILE == a"*" ]]
[root@localhost ~]# echo $?
0
[root@localhost ~]# FILE="ab"
[root@localhost ~]# [[ $FILE == a"*" ]]
[root@localhost ~]# echo $?
1
#[[]]中如果不想使用通配符*,只想表达*本身,也可以使用转义符
[root@localhost ~]# [[ $FILE == a* ]]
[root@localhost ~]# echo $?
1
[root@localhost ~]# FILE="a"
[root@localhost ~]# [[ $FILE == a* ]]
[root@localhost ~]# echo $?
1
[root@localhost ~]# FILE="a*"
[root@localhost ~]# [[ $FILE == a* ]]
[root@localhost ~]# echo $?
0

#通配符?
[root@localhost ~]# FILE=abc
[root@localhost ~]# [[ $FILE == ??? ]]
[root@localhost ~]# echo $?
0
[root@localhost ~]# FILE=abcd
[root@localhost ~]# [[ $FILE == ??? ]]
[root@localhost ~]# echo $?
1
#通配符
[root@localhost ~]# FILE=abc
[root@localhost ~]# [[ $FILE == ??? ]]
[root@localhost ~]# echo $?
0
[root@localhost ~]# FILE=abcd
[root@localhost ~]# [[ $FILE == ??? ]]
[root@localhost ~]# echo $?
1
[root@localhost ~]# NAME="linux1"
[root@localhost ~]# [[ "$NAME" == linux* ]]
[root@localhost ~]# echo $?
0
[root@localhost ~]# [[ "$NAME" == "linux*" ]]
[root@localhost ~]# echo $?
1
[root@localhost ~]# NAME="linux1*"
[root@localhost ~]# [[ "$NAME" == "linux*" ]]
[root@localhost ~]# echo $?
1
[root@localhost ~]# NAME="linux*"
[root@localhost ~]# [[ "$NAME" == "linux*" ]]
[root@localhost ~]# echo $?
0
#结论:[[ == ]] == 右侧的 * 做为通配符,不要加“”,只想做为*, 需要加“” 或转义

范例:使用 [[ ]] 判断文件后缀

[root@localhost ~]# FILE=test.log
[root@localhost ~]# [[ "$FILE" == *.log ]]
[root@localhost ~]# echo $?
0
[root@localhost ~]# FILE=test.txt
[root@localhost ~]# [[ "$FILE" == *.log ]]
[root@localhost ~]# echo $?
1
[root@localhost ~]# [[ "$FILE" != *.log ]]
[root@localhost ~]# echo $?
0

#正则表达式
[root@localhost ~]# [[ "$FILE" =~ .log$ ]]
[root@localhost ~]# echo $?
1
[root@localhost ~]# FILE=test.log
[root@localhost ~]# [[ "$FILE" =~ .log$ ]]
[root@localhost ~]# echo $?
0

范例: 判断合法的非负整数

[root@localhost ~]# N=100
[root@localhost ~]# [[ "$N" =~ ^[0-9]+$ ]]
[root@localhost ~]# echo $?
0
[root@localhost ~]# N=longwang13
[root@localhost ~]# [[ "$N" =~ ^[0-9]+$ ]]
[root@localhost ~]# echo $?
1

范例: 判断合法IP

[root@localhost ~]# IP=1.2.3.4
[root@localhost ~]# [[ "$IP" =~ ^([0-9]{1,3}.){3}[0-9]{1,3}$ ]]
[root@localhost ~]# echo $?
0
[root@localhost ~]# IP=1.2.3.4567
[root@localhost ~]# [[ "$IP" =~ ^([0-9]{1,3}.){3}[0-9]{1,3}$ ]]
[root@localhost ~]# echo $?
1
[root@localhost ~]# [[ $IP =~ ^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]]
[root@localhost ~]# echo $?
1

范例:

[root@centos7 ~]#cat check_ip.sh
#!/bin/bash
IP=$1
[[ $IP =~ ^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]
{2}|2[0-4][0-9]|25[0-5])$ ]] && echo $IP is valid || echo $IP is invalid

文件测试

存在性测试

-a FILE:同 -e
-e FILE: 文件存在性测试,存在为真,否则为假
-b FILE:是否存在且为块设备文件
-c FILE:是否存在且为字符设备文件
-d FILE:是否存在且为目录文件
-f FILE:是否存在且为普通文件
-h FILE 或 -L FILE:存在且为符号链接文件
-p FILE:是否存在且为命名管道文件
-S FILE:是否存在且为套接字文件

范例:

#文件是否不存在
[root@localhost ~]# [ -a /etc/nologin ]
[root@localhost ~]# echo $?
1
[root@localhost ~]# ![ -a /etc/nologin ]
[ -a /etc/nologin ] -a /etc/nologin ]
-bash: [: too many arguments
[root@localhost ~]# [ -a /etc/nologin ] -a /etc/nologin ]^C
[root@localhost ~]# ! [ -a /etc/nologin ]
[root@localhost ~]# echo $?
0
#文件是否存在
[root@localhost ~]# [ -a /etc/issue ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# [ ! -a /etc/issue ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# ! [ -a /etc/issue ]
[root@localhost ~]# echo $?
1
#文件是否存在
[root@localhost ~]# ! [ -e /etc/issue ]
[root@localhost ~]# echo $?
1
#此为推荐写法
[root@localhost ~]# [ ! -e /etc/issue ]
[root@localhost ~]# echo $?
1
[root@localhost ~]# [ -d /etc ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# [ -d /etc/issue ]
[root@localhost ~]# echo $?
1
[root@localhost ~]# [ -L /bin ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# [ -L /bin/ ]
[root@localhost ~]# echo $?
1

文件权限测试:

-r FILE:是否存在且可读
-w FILE: 是否存在且可写
-x FILE: 是否存在且可执行
-u FILE:是否存在且拥有suid权限
-g FILE:是否存在且拥有sgid权限
-k FILE:是否存在且拥有sticky权限

注意:最终结果由用户对文件的实际权限决定,而非文件属性决定

范例:

[root@localhost ~]# [ -w /etc/shadow ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# [ -x /etc/shadow ]
[root@localhost ~]# echo $?
1

[root@localhost ~]# [ -w test.txt ]
[root@localhost ~]# echo $?
1
[root@localhost ~]# chattr +i test.txt
chattr: No such file or directory while trying to stat test.txt
[root@localhost ~]# touch test.txt
[root@localhost ~]# chattr +i test.txt
[root@localhost ~]# lsattr test.txt 
----i-------------- test.txt
[root@localhost ~]# [ -w test.txt ]
[root@localhost ~]# echo $?
1
[root@localhost ~]# chattr -i test.txt
[root@localhost ~]# [ -w test.txt ]
[root@localhost ~]# echo $?
0

文件属性测试

-s FILE #是否存在且非空
-t fd #fd 文件描述符是否在某终端已经打开
-N FILE #文件自从上一次被读取之后是否被修改过
-O FILE #当前有效用户是否为文件属主
-G FILE #当前有效用户是否为文件属组
FILE1 -ef FILE2 #FILE1是否是FILE2的硬链接
FILE1 -nt FILE2 #FILE1是否新于FILE2(mtime)
FILE1 -ot FILE2 #FILE1是否旧于FILE2

关于 () 和 {}

(CMD1;CMD2;...)和 { CMD1;CMD2;...; } 都可以将多个命令组合在一起,批量执行

[root@centos8 ~]# man bash

( list ) 会开启子shell,并且list中变量赋值及内部命令执行后,将不再影响后续的环境
帮助参看:man bash 搜索(list)
{ list; } 不会启子shell, 在当前shell中运行,会影响当前shell环境
帮助参看:man bash 搜索{ list; }

范例: () 和 {}

[root@localhost ~]# name=long;( echo $name;name=wang;echo $name );echo $name
long
wang
long

[root@localhost ~]# name=long;{ echo $name;name=wang;echo $name; };echo $name
long
wang
wang

[root@localhost ~]# 
[root@localhost ~]# umask
0022
[root@localhost ~]# (umask 066;touch test.txt)
[root@localhost ~]# ll test.txt 
-rw-r--r--. 1 root root 0 Apr 19 20:28 test.txt

[root@localhost ~]# umask
0022

[root@localhost home]# ls
[root@localhost home]# touch test.log

[root@localhost ~]#
[root@localhost ~]# ( cd /home;ls )
test.log
[root@localhost ~]# pwd
/root
[root@localhost ~]# { cd /home;ls; }
test.log
[root@localhost home]# pwd
/home

#()会开启子shell
[root@localhost ~]# echo $BASHPID
1985
[root@localhost ~]# ( echo $BASHPID;sleep 100 )
4092
[root@localhost ~]# pstree -p
─sshd(1179)───sshd(1935)───sshd(1958)─┬─bash(1985)───bash(4092)───sleep(4093)

#{ } 不会开启子shell
[root@localhost ~]# echo $BASHPID
1985
[root@localhost ~]# { echo $BASHPID; }
1985
[root@localhost ~]#

组合测试条件

第一种方式

[ EXPRESSION1 -a EXPRESSION2 ] 并且,EXPRESSION1和EXPRESSION2都是真,结果才为真
[ EXPRESSION1 -o EXPRESSION2 ] 或者,EXPRESSION1和EXPRESSION2只要有一个真,结果就为
真
[ ! EXPRESSION ] 取反

说明: -a 和 -o 需要使用测试命令进行,[[ ]] 不支持

范例:

[root@localhost home]# [ -f $FILE -a -x $FILE ]
[root@localhost home]# echo $?
1
[root@localhost home]# [ -f $FILE -a -x $FILE ]
[root@localhost home]# echo $?
1

[root@localhost home]# touch test.log
[root@localhost home]# chmod +x test.log 
[root@localhost home]# ll
total 0
-rwxr-xr-x. 1 root root 0 Apr 19 20:47 test.log
[root@localhost home]# [ -f $FILE -a -x $FILE ]
[root@localhost home]# echo $?
0
[root@localhost home]# [ -f $FILE -o -x $FILE ]
[root@localhost home]# echo $?
0

第二种方式

COMMAND1 && COMMAND2 #并且,短路与,代表条件性的AND THEN
如果COMMAND1 成功,将执行COMMAND2,否则,将不执行COMMAND2

COMMAND1 || COMMAND2 #或者,短路或,代表条件性的OR ELSE
如果COMMAND1 成功,将不执行COMMAND2,否则,将执行COMMAND2

! COMMAND #非,取反
[root@centos7 ~]#[ $[RANDOM%6] -eq 0 ] && rm -rf /* || echo "click"

范例:

[root@localhost ~]# test "A" = "B" && echo "Strings are equal"
[root@localhost ~]# test "A" -eq "B" && echo "Integers are equal"
-bash: test: A: integer expression expected
[root@localhost ~]# test "A"-eq "B" && echo "Integers are equal"
-bash: test: A-eq: unary operator expected
[root@localhost ~]# [ "A" = "B" ] && echo "Strings are equal"
[root@localhost ~]# [ "$A" -eq "$B" ] && echo "Integers are equal"
-bash: [: : integer expression expected

[root@localhost ~]# [ -f /bin/cat -a -x /bin/cat ] && cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Fri Sep  4 21:10:02 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=232316f3-6dff-487a-9fae-af211542097b /boot                   ext4    defaults        1 2
/dev/mapper/cl-data     /data                   xfs     defaults        0 0
/dev/mapper/cl-swap     swap                    swap    defaults        0 0

[root@localhost ~]# [ -z "$HOSTNAME" -o "$HOSTNAME" = "localhost.localdomain" ] && hostname www.longwang.com

[root@localhost ~]# id long &> /dev/null || useradd long
[root@localhost ~]# id longwang &> /dev/null || useradd longwang
[root@localhost ~]# getent passwd long
long:x:1000:1000::/home/long:/bin/bash
[root@localhost ~]# grep -q no_such_user /etc/passwd || echo 'No such user'
No such user

范例:

[root@localhost ~]# [ -f "$FILE" ] && [[ "$FILE" =~ .*.sh$ ]] &&  chmod +x $FILE

[root@localhost ~]# ping -c1 -W1 172.31.0.1 &> /dev/null && echo '172.31.0.1 is up' || ( echo '172.31.0.1 is unreachable'; exit 1 )
172.31.0.1 is up

[root@localhost ~]# IP=10.0.0.100;ping -c1 -W1 $IP &> /dev/null && echo $IP is up || echo $IP is down
10.0.0.100 is down

[root@localhost ~]# IP=10.0.0.1;ping -c1 -W1 $IP &> /dev/null && echo $IP is up || echo $IP is down
10.0.0.1 is down

范例:&& 和 || 组合使用

[root@localhost ~]# NAME=long; id $NAME &> /dev/null && echo "$NAME is exist"
long is exist
[root@localhost ~]# NAME=longe; id $NAME &> /dev/null && echo "$NAME is not exist"
[root@localhost ~]# NAME=longe; id $NAME &> /dev/null || echo "$NAME is not exist"
longe is not exist
[root@localhost ~]# NAME=longe; id $NAME &> /dev/null && echo "$NAME is exist" || echo "$NAME is not exist"
longe is not exist
[root@localhost ~]# NAME=long; id $NAME &> /dev/null && echo "$NAME is exist" || echo "$NAME is not exist"
long is exist
[root@localhost ~]# NAME=long ; id $NAME &> /dev/null || echo "$NAME is not exist" && echo "$NAME is exist"
long is exist
[root@localhost ~]# NAME=longe ; id $NAME &> /dev/null || echo "$NAME is not exist" && echo "$NAME is exist"
longe is not exist
longe is exist

#结论:如果&& 和 || 混合使用,&& 要在前,|| 放在后
[root@localhost ~]# NAME=longe; id $NAME &> /dev/null && echo "$NAME is exist" || useradd $NAME
[root@localhost ~]# id longe
uid=1002(longe) gid=1002(longe) groups=1002(longe)
[root@localhost ~]# NAME=longee; id $NAME &> /dev/null && echo "$NAME is exist" || ( useradd $NAME; echo $NAME is created )
longee is created
[root@localhost ~]# NAME=longgge; id $NAME &> /dev/null && echo "$NAME is exist" || { useradd $NAME; echo $NAME is created }
> ^C
[root@localhost ~]# NAME=longgge; id $NAME &> /dev/null && echo "$NAME is exist" || { useradd $NAME; echo $NAME is created; }
longgge is created

范例:网络状态判断

[root@localhost ~]# cat ping.sh 
#!/bin/bash
IP=172.31.0.1
ping -c1 -W1 $IP &> /dev/null && echo "$IP is up" || { echo "$IP is unreachable"; exit; }
echo "Script is finished"
[root@localhost ~]# bash ping.sh 
172.31.0.1 is up
Script is finished

范例:磁盘空间的判断

[root@localhost ~]# cat disk_check.sh 
#!/bin/bash
WARNING=80
SPACE_USED=`df | grep '^/dev/sd' | tr -s ' ' % | cut -d% -f5 | sort -nr | head -n1`
[ "$SPACE_USED" -ge $WARNING ] && echo "disk used is $SPACE_USED,will be full" | mail -s diskwaring root

范例:磁盘空间和Inode号的检查脚本

[root@localhost ~]# cat disk_check1.sh 
#!/bin/bash
WARNING=80
SPACE_USED=`df | grep '^/dev/sd' | grep -oE '[0-9]+%' | tr -d % | sort -nr | head -n1`
INODE_USED=`df -i | grep '^/dev/sd' | grep -oE '[0-9]+%' | tr -d % | sort -nr | head -n1`
[ "$SPACE_USED" -gt $WARNING -o "$INODE_USED" -gt $WARNING ] && echo "DISK USED:$SPACE_USED%,INODE_USED:$INODE_USED,will be full" | mail -s "DISK Warning"
原文地址:https://www.cnblogs.com/xuanlv-0413/p/14881427.html