Reputation: 21393
I am having a simple script that does system updates and sends mail about the status like this:
echo '===============================\n' > mylog.log
sudo apt-get update -y >> mylog.log
if [ $? -eq 0 ]; then
echo 'completed successfully\n' >> mylog.log
else
echo 'FAILED\n' >> mylog.log
fi
echo '===============================' >> mylog.log
sudo apt-get dist-upgrade -y >> mylog.log
if [ $? -eq 0 ]; then
echo 'completed successfully\n' >> mylog.log
else
echo 'FAILED\n' >> mylog.log
fi
echo 'sending mail\n' >> mylog.log
mailx -aFrom:[email protected] -a 'Content-Type: text/html' -s "Updates @`date`" [email protected] < mylog.log
The program is working fine and I am getting mail. But the mail is not having any new lines. Here is the content of mail:
=============================== 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
The mail is displayed in single line which is not in readable format. If I open the log file mylog.log, it is in proper format with new lines.
===============================
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
How can I send mail in proper readable format?
Update: Used printf in code, but same issue.
printf '%b' '===============================\r\n' > mylog.log
sudo apt-get update -y >> mylog.log
if [ $? -eq 0 ]; then
printf '%b' 'completed successfully\r\n' >> mylog.log
else
printf '%b' 'FAILED\n' >> mylog.log
fi
printf '%b' '===============================' >> mylog.log
sudo apt-get dist-upgrade -y >> mylog.log
if [ $? -eq 0 ]; then
printf '%b' 'completed successfully\r\n' >> mylog.log
else
printf '%b' 'FAILED\n' >> mylog.log
fi
printf '%b' 'sending mail\n' >> mylog.log
mailx -aFrom:[email protected] -a 'Content-Type: text/html' -s "Updates @`date`" [email protected] < mylog.log
Upvotes: 0
Views: 65
Reputation: 189307
You are declaring the content to be HTML, but apparently what you are sending is a simple text file. Either add HTML formatting to have it display as expected, or send it as text/plain
instead. (Personally, I would emphatically suggest the latter.)
Upvotes: 1
Reputation: 322
Do
echo -e '===============================\n'
By default bash will not interpret backslashes .
Upvotes: 0