3

I have a script that emails the text of a log file as the body of the email. That log file has some blank lines between lines of text for readability.

When I use the following line in the script, the email sends fine with proper formatting:

cat ${log}|mailx -s "Email Subject Line" ${i} < ${log}

However, I now want to attached several files to the email. I modified the line to this:

( echo `cat ${log}` ; uuencode ${file1} ${file1a} ; uuencode ${file2} ${file2a} ; uuencode ${file3} ${file3a} ) | mailx -m -s "Email Subject Line" ${i}

Note that ${i} is the email recipient in both lines.

So, now the email sends fine with the attachments, but the body of the message is just one big block of text with no line breaks or anything.

Did I not write the mailx line correctly? Is there a way I can get the formatting to persist into the body of the message?

| improve this question | | | | |
1

Unquoted newlines in command lines get treated as spaces; that's why you can say things like

command1  &&
command2

Well, sometimes unquoted newlines get treated like semicolons.  But, when you say

echo `cat ${log}`

all the newlines in the log file get turned into spaces.  You could fix this by saying

echo "`cat ${log}`"

but why not just say

cat ${log}

?

| improve this answer | | | | |
  • Thank you for the response! Adding double quotes fixed it. Removing the back ticks didn't work. I had tried that before and it just prints the file path of the log as the body of the email. But the double quotes did it! Thanks again! – user3756852 Oct 17 '14 at 12:47
  • I'm glad I was able to provide a solution that helped you, but … are you sure you copied my last command exactly? I didn't say echo ${log} or echo cat ${log} (which is what you wrote in the first version of your question). I said cat ${log}, with no echo. (Although I would actually advise cat "${log}", to be safer. You should always quote shell variables unless you have a good reason not to, and you're sure you know what you're doing. By contrast, while braces can be important, they're not as important as quotes, so cat "$log" is good enough. Similarly, "$file1", etc.) – G-Man Says 'Reinstate Monica' Oct 17 '14 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.