Bash:通过电子邮件发送文件内容,在邮件上收到空间问题

时间:2018-03-13 07:30:36

标签: linux bash shell centos7

我正在使用简单的bash脚本,该脚本将值存储在文件中,并将这些文件内容发送到相应的电子邮件中。

bash命令的输出如下: -

shell command output

但是当我发送此输出时,我就像这样

email output

它的格式不正确。 shell脚本代码如下 -

  

ps -eo pid,ppid,cmd,%mem,%cpu --sort = - %mem |头-4>> /tmp/abc.txt。

     

mail -s“mail”abc@gmail.com< /tmp/abc.txt

请帮我解决这个格式问题。 我是bash脚本的新手。

2 个答案:

答案 0 :(得分:1)

您在邮件客户端中使用比例字体。将其配置为以固定宽度格式显示纯文本,或者使用一些丰富的格式,您可以对此进行更多控制。

顺便说一句,没有必要使用临时文件进行输出。

ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -4 |
mail -s"mail" abc@gmail.com

这是一个粗略的尝试,使其成为固定宽度的HTML:

( printf '<html><body><pre>'
  ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -4
  printf '</pre></body></html>\n'
) | mail -s"mail" -a 'Content-type: text/html' abc@gmail.com

-a的{​​{1}}选项不可移植;如果您不在mail允许您控制邮件的MIME -A的平台上,则可以尝试使用mutt或使用mail -a。另请参阅Mailx send html message

答案 1 :(得分:0)

认为它应该保留空格:

#!/bin/bash
stdout=`ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -4`
echo "<html><body><pre>${stdout}</pre></body></html>" >> ./test.html
mail -a "Content-type: text/html" -s "mail" abc@gmail.com < ./test.html

看起来很相似:

  PID  PPID CMD                         %MEM %CPU
 2583     1 /usr/java/jdk1.8.0_162/jre/ 20.8  0.5
28660 20791 /home/google/android-studio 18.2  5.9
20791 20720 /home/google/android-studio  9.8 25.8