【问题标题】:How to redirect multiline output into mailx body of message?如何将多行输出重定向到邮件的mailx正文?
【发布时间】:2022-01-11 22:00:17
【问题描述】:

我正在尝试将命令的输出重定向到 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

但它以附件形式出现的二进制文件(ATT00001.bin)

当我跑步时

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

并将该文件重定向到 mailx 我在一个衬里收到电子邮件:

thefirstlinethesecondline

什么时候应该有两个

thefirstline
thesecondline

【问题讨论】:

  • 你能read -r -d '' content &lt; tempmsg; printf '%q\n' "$content" 并将输出添加到问题中吗?
  • @fravadona 输出已添加。谢谢
  • 在 UNIX 术语中,不以 \n 字符结尾的文件被视为二进制文件。 "$curccrtpick" 的最后一个 \n 已在代码中的某处被删除
  • @fravadona 我已经更新了最新结果。
  • 您可以使用echo &gt;&gt; tempmsg1 快速解决问题,但您应该搜索最后一个换行符消失的位置(可能在$( ) 中)

标签: bash centos8 mailx


【解决方案1】:

这是对我有用的逻辑。使用 sendmail 似乎比使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 2012-01-11
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    相关资源
    最近更新 更多