通过电子邮件发送diff输出和邮件正文

时间:2017-04-12 17:36:02

标签: linux email unix diff

我的Bash脚本中有以下代码。如何在电子邮件中添加正文?  示例:正文:输出

diff -q file1 file2 || diff file1 file2 | mail -s "subject" "email@email.com"

2 个答案:

答案 0 :(得分:0)

假设您有mail程序了解-a附加文件,例如s-nail或mailx,

if ! diff -q file1 file2; then
    diff file1 file2 > something.diff
    echo 'body' | mail -s 'subject' -a something.diff 'email@email.com'
    rm -f something.diff
fi

您可以使用Bash的进程替换来避免临时文件

diff -q file1 file2 || echo 'body' | mail -s 'subject' -a <(diff file1 file2) 'email@email.com'

但附件的文件名只是一些随机数。

答案 1 :(得分:0)

这就是我做的事情

difference=` diff file1 file2 >/dev/null || diff file1 file2`
   if [ -n "$difference" ]; then
mail -s " subject " "address" <<EOF
     Body here

     $difference

      Body here

  EOF
 fi