How to redirect multiline output into mailx body of message?

537 Views Asked by At

I'm trying to redirect the output of a command into mailx.

if grep -B 1 line $curccrtpick; then
    grep -B 1 line $curccrtpick | head -2 > tempmsg
    mail -s "String found in $curccrtpick" -r [email protected] [email protected] < tempmsg
    #rm tempmsg
else
    echo nothing
fi

But it appears as a binary file in the form of an attachment (ATT00001.bin)

When I run

tr -d '[\015\200-\377]' < tempmsg > tempmsg1

and redirect that file into mailx I receive the email in a one liner:

thefirstlinethesecondline

when there should be two

thefirstline
thesecondline

1

There are 1 best solutions below

0
vicscul On

This is the logic that worked for me. Utilizing sendmail seems more appropriate than mailx.

if grep -B 1 Retry $curpickccsb; then
        stringLine1=$(grep -B 1 Line $curpickccsb | head -1)
        stringLine2=$(grep Line $curpickccsb | head -1)
        echo -e "String located in $curpickccsb. Sending email notification."
        echo -e "Content-Type: text/plain\r\nFrom: [email protected]\r\nSubject: String in $curpickccsb\r\n\r\n'$stringLine1'\r\n'$stringLine2'\r\n" | sendmail -f [email protected] [email protected]
else
        echo -e "No errors located in $curpickccsb"
fi