0

我创建了一个 .sh 文件来监视 2 个文件路径并将磁盘大小发送给我。它运行,我没有收到邮件。文件系统 > 90%

#!/bin/bash
used=$(df -Ph | grep 'location1' | awk {'print $5'}) 
used1=$(df -Ph | grep '/location2' | awk {'print $5'}) 
max=80% 
if [ ${used%?} -gt ${max%?} ]; then mail -s 'Disk space alert' abc@eee.com;bbb@eee.com << EOF
The Mount Point "location1" on $(hostname) has used $used at $(date);
if [ ${use1%?} -gt ${max%?} ]; then mail -s 'Disk space alert' abc@eee.com; bbb@eee.com << EOF
The Mount Point "location2" on $(hostname) has used $used1 at $(date);

EOF
fi
4

2 回答 2

0

谢谢大家,我已经能够弄清楚了。

#!/bin/bash
used=$(df -Ph | grep 'location1' |  awk '{print $5}' | sed 's/%//g' ) 

used1=$(df -Ph | grep 'location2' |  awk '{print $5}' | sed 's/%//g' ) 

max=80% 
if [ ${used%?} -gt ${max%?} ]; then 

if [ ${use1%?} -gt ${max%?} ]; then 
mail -s 'Disk space alert' abc@eee.com bbb@eee.com << EOF

The Mount Point 'location2' on $(hostname) has used $used1 at $(date);

EOF
fi
fi
于 2021-10-04T07:01:49.970 回答
0

您未能包含EOF标记,因此其余代码隐藏在此处的文档中。(您问题中的语法着色应该可以帮助您注意。)

顺便说一句,您想避免无用grep并修复缩进。我还猜测您不希望第二个if仅在第一个触发时才触发。

#!/bin/bash
used=$(df -Ph | awk '/location1/ { print $5}') 
used1=$(df -Ph | awk '/\/location2/ { print $5}') 
max=80%
if [ ${used%?} -gt ${max%?} ]; then
  mail -s 'Disk space alert' abc@eee.com;bbb@eee.com <<__EOF
The Mount Point "location1" on $(hostname) has used $used at $(date);
__EOF
fi
if [ ${use1%?} -gt ${max%?} ]; then
  mail -s 'Disk space alert' abc@eee.com; bbb@eee.com <<__EOF
The Mount Point "location2" on $(hostname) has used $used1 at $(date);
__EOF
fi

具有较少代码重复的更惯用的解决方案将循环参数。

#!/bin/bash
max=80
for mountpoint in location /location2; do
    used=$(df -Ph "$mountpoint" | awk 'NR>1 { print $5}') 
  if [ ${used%?} -gt $max ]; then
    mail -s 'Disk space alert' abc@eee.com;bbb@eee.com <<____EOF
The Mount Point "$mountpoint" on $(hostname) has used $used at $(date);
____EOF
  fi
done
于 2021-10-04T07:24:19.393 回答