问题
This should be relatively simple but I've never run into it before and now I'm scratching my head.
Simple script:
echo `tail /var/log/qmailcheck.log` >> $EMAIL
cat $EMAIL | mail -s "Daily server report from server.ca" email@here.com
(I know I'm missing a set of ticks around the tail
cmd just wanted things to look right)
The problem is that although the log file I'm tailing out has a new line for every entry the email that gets sent puts everything onto one line so instead of this:
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
I get this: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
The script also echo's from a number of other commands and they also seem to run into one line as well, I've done this before but never had an issue, what am I missing?
Here's the entire script:
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
回答1:
That's what you get for using echo and command substitution when you don't need either of them.
The only distinction between
echo `command args...`
and
command args
is that the newlines are collapsed (and the code is harder to read). Just use
tail /var/log/qmailcheck.log | mail -s ....
(If you want to use an intermediate file, then you can use it too without echo
or backticks).
Note that there is another style for command substitution: $(command...)
, which gets rid of problems with nested quoting and is generally more readable. Whenever you want command substitution, you can use this style instead of backticks.
回答2:
Change:
echo `tail /var/log/qmailcheck.log` >> $EMAIL
to:
echo "`tail /var/log/qmailcheck.log`" >> $EMAIL
Wrapping the command substitution with double quotes prevents string splitting, which is where the newline conversion to space occurs.
However, I recommend going with Anton Kovalenko's solution. There's no need to use echo
here at all, just redirect the command directly to the file.
来源:https://stackoverflow.com/questions/14693382/all-lines-running-together-in-email