这应该相对简单,但我以前从未遇到过它,现在我摸不着头脑。
简单的脚本:
echo `tail /var/log/qmailcheck.log` >> $EMAIL
cat $EMAIL | mail -s "Daily server report from server.ca" email@here.com
(我知道我在 tail
cmd 周围遗漏了一组刻度线,只是想让事情看起来正确)
问题是,虽然我拖出的日志文件的每个条目都有一个新行,但发送的电子邮件将所有内容都放在一行中,所以不是这样:
Mon Feb 4 11:05:01 MST 2013-- Check Completed Successfully<br/>
Mon Feb 4 11:10:01 MST 2013-- Check Completed Successfully<br/>
Mon Feb 4 11:15:01 MST 2013-- Check Completed Successfully<br/>
Mon Feb 4 11:20:01 MST 2013-- Check Completed Successfully<br/>
Mon Feb 4 11:25:01 MST 2013-- Check Completed Successfully
我明白了:
Mon Feb 4 11:05:01 MST 2013-- Check Completed Successfully Mon Feb 4 11:10:01 MST 2013-- Check Completed Successfully Mon Feb 4 11:15:01 MST 2013-- Check Completed Successfully Mon Feb 4 11:20:01 MST 2013-- Check Completed Successfully Mon Feb 4 11:25:01 MST 2013-- Check Completed Successfully
该脚本还从许多其他命令中回显,它们似乎也出现在一行中,我以前这样做过但从来没有遇到过问题,我错过了什么?
这是完整的脚本:
ERROR=/tmp/errorlog
RECIP=$(cat /root/bin/emailrec)
echo Hi, this email to inform you of any potential issues with the server.bla qmail mail server > /tmp/demail
grep pls /var/log/qmailcheck.log > /tmp/errorlog
if [ ! -s $ERROR ] ; then
echo There are no errors in the qmailcheck log file >> /tmp/demail
echo Here are the last 10 lines of the current qmailcheck log file: >> /tmp/demail
tail /var/log/qmailcheck.log >> /tmp/demail
else
echo The log file for the qmail check script contains the following errors: >> /tmp/demail
cat $ERROR >> /tmp/demail
echo You should have also received an email which will better explain the error >> /tmp/demail
echo Check the time of the error above to determine when the email was sent >> /tmp/demail
fi
MAIL=$(/var/qmail/bin/qmail-qstat | grep "queue:" | awk '{ print $4 }')
echo There are $MAIL messages in the mail queue >> /tmp/demail
echo File system usage is currently at `df -h |grep vzfs | awk '{ print $5}'` >> /tmp/demail
cat /tmp/demail | mail -s "Daily server report from server.bla" $RECIP
最佳答案
这就是您在不需要 echo 和 命令替换时获得的结果。
唯一的区别
echo `command args...`
和
command args
是换行符被折叠(代码更难阅读)。只需使用
tail /var/log/qmailcheck.log | mail -s ....
(如果你想使用一个中间文件,那么你也可以在没有 echo
或反引号的情况下使用它)。
请注意,还有另一种命令替换样式:$(command...)
,它消除了嵌套引用的问题并且通常更具可读性。每当您需要命令替换时,您都可以使用这种样式而不是反引号。
关于linux - 电子邮件中的所有行一起运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14693382/