简体   繁体   中英

How to redirect multiline output into mailx body of message?

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 test@test.com dummy@test.com < 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

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: no-reply@domain.com\r\nSubject: String in $curpickccsb\r\n\r\n'$stringLine1'\r\n'$stringLine2'\r\n" | sendmail -f no-reply@domain.com test@domain.com
else
        echo -e "No errors located in $curpickccsb"
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM