1

i am using simple bash script which stores values in file and those content of file sends to appropriate email.

the output of bash command comes like this:-

shell command output

but when i sends this output, i am getting like this

email output

Its not in proper format. shell script code is as follows-

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

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

Please help me to sort out this formatting issue. I am new to bash scripting.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • It looks like the output does contain tabs and spaces just as the bash output, so perhaps your issue is more to do with the font? – itChi Mar 13 '18 at 07:41

2 Answers2

2

You are using a proportional font in your mail client. Configure it to show plain text in fixed-width formatting, or perhaps use some rich format where you have more control over this.

Incidentally, there is no need to use a temporary file for the output.

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

Here's a crude attempt at making it fixed-width 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

The -a option to mail is not portable; perhaps try with -A or use mutt instead if you are not on a platform where mail -a allows you to control the MIME Content-Type of your message. See also Mailx send html message

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

think it should preserve the spaces:

#!/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

looks alike this:

  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
Martin Zeitler
  • 1
  • 19
  • 155
  • 216