我有一个简单的脚本,它可以进行系统更新并发送关于状态的邮件,如下所示:
echo '===============================n' > mylog.log
sudo apt-get update -y >> mylog.log
if [ $? -eq 0 ]; then
echo 'completed successfullyn' >> mylog.log
else
echo 'FAILEDn' >> mylog.log
fi
echo '===============================' >> mylog.log
sudo apt-get dist-upgrade -y >> mylog.log
if [ $? -eq 0 ]; then
echo 'completed successfullyn' >> mylog.log
else
echo 'FAILEDn' >> mylog.log
fi
echo 'sending mailn' >> mylog.log
mailx -aFrom:test@mymail.com -a 'Content-Type: text/html' -s "Updates @`date`" chaitanya@mymail.com < mylog.log
程序运行良好,我正在收到邮件。但是邮件没有任何新的线路。以下是邮件内容:
=============================== Hit http://security.ubuntu.com precise-security/multiverse Translation-en Hit http://security.ubuntu.com precise-security/restricted Translation-en Hit http://security.ubuntu.com precise-security/universe Translation-en Reading package lists... completed successfully =============================== Reading package lists... Building dependency tree... Reading state information... 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. completed successfully sending mail
邮件以单行形式显示,这不是可读的格式。如果我打开日志文件mylog.log,它的格式是正确的,有新行。
===============================
Hit http://security.ubuntu.com precise-security/multiverse Translation-en
Hit http://security.ubuntu.com precise-security/restricted Translation-en
Hit http://security.ubuntu.com precise-security/universe Translation-en
Reading package lists...
completed successfully
===============================
Reading package lists...
Building dependency tree...
Reading state information... 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
completed successfully
sending mail
如何以正确可读的格式发送邮件?
更新:在代码中使用了printf,但问题相同。
printf '%b' '===============================rn' > mylog.log
sudo apt-get update -y >> mylog.log
if [ $? -eq 0 ]; then
printf '%b' 'completed successfullyrn' >> mylog.log
else
printf '%b' 'FAILEDn' >> mylog.log
fi
printf '%b' '===============================' >> mylog.log
sudo apt-get dist-upgrade -y >> mylog.log
if [ $? -eq 0 ]; then
printf '%b' 'completed successfullyrn' >> mylog.log
else
printf '%b' 'FAILEDn' >> mylog.log
fi
printf '%b' 'sending mailn' >> mylog.log
mailx -aFrom:test@mymail.com -a 'Content-Type: text/html' -s "Updates @`date`" chaitanya@mymail.com < mylog.log
您声明的内容是HTML,但显然您发送的是一个简单的文本文件。添加HTML格式以使其按预期显示,或者将其作为text/plain
发送。(就我个人而言,我强烈建议后者。)
我建议使用:
printf '%b' 'textrn'
执行
echo -e '===============================n'
默认情况下,bash不会解释反斜杠。