Bash-script: running command and pipeing it to mail gives mail without line breakse

Posted on 2014-04-26
Last Modified: 2014-04-27
I have a bash script that needs to do this:

1) Run a external command and store the output either in variable or tempfile.
2) Add the output as a body in a mail with the mail-command.

The mail body needs to contain static text both before and after the output from the external command which excludes the solution to pipe it directly by

external-command | mail

I have tried this:
1) Storing output of external-command in a variable by running
and creating another variable that contains static text+variable+static text and run
echo($anothervariable) | mail
2) pipeing the external-command output to a tempfile and do
(cat pretext; cat tempfile; cat posttext) | mail ...

Both my solutions above gives me a mail without line breaks with makes it unusable.

Please advice how to send my text with proper line breaks.

Best regards
Question by:Kvistofta
  • 4
  • 3
  • 2
  • +1
LVL 19

Expert Comment

ID: 40025026
Whenever you reference the variable, enclose it in double quotes, otherwise the shell will swallow any internal end-of-line markers.  e.g.

    echo "$anothervariable" | mail
LVL 24

Expert Comment

ID: 40025030
Revisit the nature of the breaks with regard to production of newline, carriage returns, even for checking the hex or decimal value of character. For variables you can try to wrap them with double quotes.
LVL 19

Expert Comment

ID: 40025034
Another option would be

   ( cat pretext ; external-command; cat posttext) | mail

If you do have the data in a file, you can use a single "cat" command

    cat pretext tempfile posttext | mail

(a command actually using "cat" for its intended purpose - to concatenate two or more files!)
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

LVL 62

Expert Comment

ID: 40025182
It is much simpler... No variables no tempfiles
(echo C1 START ; command1 && echo C1 FINISH || echo C1 FAIL ; .... C2) | mail -s "REPORT `date`" you@work
LVL 17

Author Comment

ID: 40025391
Putting the variable between ":s does not help. This is how my script looks like right now:

                commandoutput=`ls -l /usr`
                echo Output from external command:
                echo -----------
                echo "$commandoutput"
                echo -----------
                echo Length of result: $outputlength
                if [ "$outputlength" -gt "20" ] ; then
                        echo "$(date +%H:%M:%S) : Result is $outputlength bytes, sending mail to $email!" >> $LOGFILE
                        SUBJECT="Email notification from CHANGE regarding zone $domain"

                        (echo "Line1\r\n"; \
                         echo "Line2\r\n"; \
                         echo "$commandoutput\r\n"; \
                         echo "Line3\r\n"; \
                         echo "Line4\r\n";) \
                         | mail \
                                                -a "From: $EMAILFROM" \
                                                -a "MIME-Version: 1.0" \
                                                -a "Content-Type: text/html" \
                                                -s "$SUBJECT" \

Open in new window

The output on the screen looks good:

Output from external command:
total 100
drwxr-xr-x   2 root root 40960 Apr 24 12:04 bin
drwxr-xr-x   2 root root  4096 Apr 24 09:11 games
drwxr-xr-x  32 root root 16384 Apr 24 12:04 include
drwxr-xr-x  67 root root 12288 Apr 24 12:04 lib
drwxr-xr-x  10 root root  4096 Mar  3 13:33 local
drwxr-xr-x   2 root root 12288 Apr 24 12:04 sbin
drwxr-xr-x 124 root root  4096 Apr 24 12:04 share
drwxr-xr-x   4 root root  4096 Apr 24 12:04 src
Length of result: 404

Open in new window

However, the email looks weird. Here is a screen dump from the receivers gmail:

Screendump from gmail
Not even my \r\n makes proper line breaks....?
LVL 62

Expert Comment

ID: 40025466
You dont need to use \r \n because ECHO will add proper line nedings without it.
LVL 19

Expert Comment

ID: 40025587
One problem with your code is that echo needs the "-e" option for it to interpret backslash sequences - as you can see from your screen capture, your "\r\n" in the echo has been sent as 4 separate characters (backslash, r, backslash and n).  so
    echo -e "${LINE1}\r\n"
would do what you are trying to do.

"echo" will add a \n to the end of the line (unless you use the "-n" flag on it), so you don't need to put that in, just the \r:
    echo -e "${LINE1}\r"

Another problem is the line echoing "$commandoutput" - you add a carriage-return and linefeed at the end of the variable, but that does nothing to change the linefeeds within the variable (i.e. between the lines that make up the "ls" output).

If you want to have \r\n line endings on your mail, a better solution would be the unix2dos command (in the "dos2unix" package on Ubuntu, or the "unix2dos" on RedHat).  Just use normal UNIX line endings on your echo commands, then pipe the result through unix2dos before passing to the mail program:
(echo "Line1"; \
                         echo "Line2"; \
                         echo "$commandoutput"; \
                         echo "Line3"; \
                         echo "Line4";) \
                         | unix2dos | mail \
                                                -a "From: $EMAILFROM" \

Open in new window

That said, none of the above is required here!
LVL 19

Accepted Solution

simon3270 earned 500 total points
ID: 40025589
Your problem is the

    -a "Content-Type: text/html"

This tells the receiving email client that your email is in HTML (ugh!), but it isn't!

To fix, either make it HTML (easiest would be to add a <pre> tag at the start, and </pre> at the end), or change that header item to say it's plain text.

    -a "Content-Type: text/plain"
LVL 62

Expert Comment

ID: 40025593
Internet standard is unix line breaks. echo adds them without escapes.

-a parameter is obsolete as text/plain is assumed if nothing is set...
LVL 17

Author Comment

ID: 40026610
Weeeh! I have tried all sorts of recommended solutions above when it comes to adding/changing line breaks but nothing happened. Until I got noticed that I used text/html in the header. Duh! Sorry folks! I have learned a lot from all of your comments and suggested solutions above.

Best regards

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle Finace 3 77
Using init script with Systemd: need help crafting unit file 11 111
aix unix tar error 3 67
awk file 6 54
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question