当我将猫传送到邮件时,遇到了一个奇怪的问题。
我有一份由脚本编译而成的简单文本文件,它是一份关于脚本中复制了哪些文件夹的报告。然后我想通过电子邮件将此报告发送给系统管理员。
脚本可以很好地生成报告。当我运行
cat /tmp/report.txt
我获得了格式良好的输出,其中包含我期望的所有换行符。
Copy report. Started on Mon Sep 28 13:37:57 BST 2015
Run in progress?: Yes
3 directories were copied.
Copy started on 2015.09.28 at 13:37:57
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10)
Copy completed on 2015.09.28 at 13:37:57
Copy info:
0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k
0inputs+355896outputs (0major+150minor)pagefaults 0swaps
Copy started on 2015.09.28 at 13:37:59
Copying /mnt/smb/New folder (14) to /mnt/backup/New folder (14)
Copy completed on 2015.09.28 at 13:37:59
Copy info:
0.00user 0.41system 0:01.78elapsed 23%CPU (0avgtext+0avgdata 2620maxresident)k
0inputs+355896outputs (0major+152minor)pagefaults 0swaps
Copy started on 2015.09.28 at 13:38:01
Copying /mnt/smb/New folder (18) to /mnt/backup/New folder (18)
Copy completed on 2015.09.28 at 13:38:01
Copy info:
0.00user 0.28system 0:01.71elapsed 16%CPU (0avgtext+0avgdata 2768maxresident)k
0inputs+355896outputs (0major+151minor)pagefaults 0swaps
然而,出于某种原因,当我现在将其发送到邮件时
cat /tmp/report.txt | mail -s "Copy Report" [email protected]
或者
mail -s "Copy Report" [email protected] < /tmp/report.txt
这是我收到的电子邮件。
Copy report. Started on Mon Sep 28 13:37:57 BST 2015
Run in progress?: Yes
3 directories were copied.
Copy started on 2015.09.28 at 13:37:57
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10) Copy completed on 2015.09.28 at 13:37:57 Copy info:
0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k
0inputs+355896outputs (0major+150minor)pagefaults 0swaps
Copy started on 2015.09.28 at 13:37:59
Copying /mnt/smb/New folder (14) to /mnt/backup/New folder (14) Copy completed on 2015.09.28 at 13:37:59 Copy info:
0.00user 0.41system 0:01.78elapsed 23%CPU (0avgtext+0avgdata 2620maxresident)k
0inputs+355896outputs (0major+152minor)pagefaults 0swaps
Copy started on 2015.09.28 at 13:38:01
Copying /mnt/smb/New folder (18) to /mnt/backup/New folder (18) Copy completed on 2015.09.28 at 13:38:01 Copy info:
0.00user 0.28system 0:01.71elapsed 16%CPU (0avgtext+0avgdata 2768maxresident)k
0inputs+355896outputs (0major+151minor)pagefaults 0swaps
怎么回事?有些换行符在那里,有些却不在!我尝试在 vi 和 nano 中编辑文件,并确保换行符确实在那里。还删除了它们并重新创建它们,电子邮件输出仍然相同。
使用 /usr/bin/mail
更多信息:
我手动将以下内容输入到文本文件中,然后通过同样的管道发送到邮件。我在每行开头都加上了“复制”一词。
Copy started on 2015.09.28 at 13:37:57
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10)
Copy completed on 2015.09.28 at 13:37:57
Copy info:
Copy 0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k
Copy 0inputs+355896outputs (0major+150minor)pagefaults 0swaps
这是我在电子邮件中收到的内容:
Copy started on 2015.09.28 at 13:37:57
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10) Copy completed on 2015.09.28 at 13:37:57 Copy info:
0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k
0inputs+355896outputs (0major+150minor)pagefaults 0swaps
然后我删除了所有“复制”字样。这是我在电子邮件中收到的内容。
Started on 2015.09.29 at 09:46:49
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10) Completed on 2015.09.29 at 09:46:49
Info:
0.00user 0.46system 0:01.98elapsed 23%CPU (0avgtext+0avgdata 2552maxresident)k
0inputs+355896outputs (0major+149minor)pagefaults 0swaps
我认为这可能与“复制”一词有关,但我仍然有一行没有换行符!
这是输出cat -vet /tmp/report.txt
Copy report. Started on Mon Sep 28 13:37:57 BST 2015$
$
Run in progress?: Yes$
$
$
$
3 directories were copied.$
$
$
Copy started on 2015.09.28 at 13:37:57$
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10)$
Copy completed on 2015.09.28 at 13:37:57$
Copy info:$
0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k$
0inputs+355896outputs (0major+150minor)pagefaults 0swaps$
$
$
Copy started on 2015.09.28 at 13:37:59$
Copying /mnt/smb/New folder (14) to /mnt/backup/New folder (14)$
Copy completed on 2015.09.28 at 13:37:59$
Copy info:$
0.00user 0.41system 0:01.78elapsed 23%CPU (0avgtext+0avgdata 2620maxresident)k$
0inputs+355896outputs (0major+152minor)pagefaults 0swaps$
$
$
Copy started on 2015.09.28 at 13:38:01$
Copying /mnt/smb/New folder (18) to /mnt/backup/New folder (18)$
Copy completed on 2015.09.28 at 13:38:01$
Copy info:$
0.00user 0.28system 0:01.71elapsed 16%CPU (0avgtext+0avgdata 2768maxresident)k$
0inputs+355896outputs (0major+151minor)pagefaults 0swaps$
答案1
你可能正遭受“format=flowed”的困扰特征很久以前介绍过,看看rfc2646。
您可能会发现有些行末尾有空格字符。有些邮件阅读器会将其解释为“将此行与下一行连接起来,然后重新对文本进行换行,使行适合屏幕宽度”。
Content-Type
通常,只有当您的邮件具有标题选项时才应执行此操作format=flowed
,但现在,由于非 Unix 人群的存在,这已成为默认设置。
使用cat -vet
来查看数据中是否有尾随空格。使用sed 's/ \+$//'
来删除它们。
我没有在您的数据中看到任何尾随空格、控制字符和回车符,因此您的邮件程序没有理由篡改它。
最后一次尝试:按照 Andrzej A. Filip 的建议,不要mail
直接转到sendmail
。这将允许您添加显式的内容类型标头。例如:
(echo 'Subject: Copy Report'
echo 'Content-Type: text/plain; charset=us-ascii; format=fixed'
echo
cat /tmp/report.txt
) | sendmail [email protected]
最后echo
是在数据之前结束标题。sendmail 可能不在您的 PATH 中,请在 /sbin 和 /usr/sbin 等中查找。