仅当存在差异时才通过电子邮件发送两个文件之间的差异输出

emailing diff output between two files only if there is a difference

如何通过电子邮件发送 file1 和 file2 之间的输出,但前提是存在差异?假设我正在使用 shell 脚本。

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

我在网上找到了这个,效果很好

command > file
if [ -s file ]
then
   mailx -s "Subject" mailaddress <file.tmp
fi
rm file

使用||串联。

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

更多信息here