Bash脚本即使不应该发送电子邮件也会发送电子邮件

时间:2017-05-03 02:23:48

标签: bash email debian-jessie tripwire

我每小时都有一个由root运行的cron作业,用于检查是否存在tripwire违规行为。它仍然每小时向我发送一封电子邮件,无论我是否有违规行为。如果存在违规行为,则会包含该报告。如果没有违规行为,它会向我发送一封空白电子邮件,其中只包含主题行。

这是脚本:

#!/bin/bash

# Save report
tripwire --check > /tmp/twreport

# Count violations
v=`grep -c 'Total violations found:  0' /tmp/twreport`

# Send report
if [ "$v" -eq 0 ]; then
        mail -s "[tripwire] Report for `uname -n`" user@example.com < /tmp/twreport
fi

2 个答案:

答案 0 :(得分:0)

我建议将代码更改为

if [ -f /tmp/twreport ] # Check file exists
then
 v=$(grep -c '^Total violations found:  0$' /tmp/twreport)
 #Not suggested using legacy backticks
 if [ "$v" -eq 0 ]; then
        mail -s "[tripwire] Report for $(uname -n)" user@example.com < /tmp/twreport
 fi
fi

最后在放置脚本行之前在cron中设置路径。像

# Setting PATH
PATH=/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/path/to/tripwire:/and/so/on
# Now,set up the cron-job for the script
0        11         *              *          0       /path/to/script

答案 1 :(得分:0)

尝试用双引号括起来并使用完整路径

v="`/bin/grep -c 'Total violations found:  0' /tmp/twreport`"

if [ "$v" == "0" ]; then # or = instead of == based on your shell

如果这些不起作用,请验证搜索字词。我在'found:0'<0>的0之前看到两个空格

相关问题