Shell Script email output formatting

0

I am trying to create a shell script that echo's a bunch of commands and have gotten it to send out emails fairly simply.

{
  echo $(hostname)
  echo ""
  echo "Uptime" $(uptime)
  echo ""
  echo "Who is on the server?" $(who)
  echo ""
  echo "Recent logs" $(last)
}| mail -s "Homework Report" myemail@gmail.com

The outputs on the emails are all in a single sentence, raw format:

myhostname.com

Uptime 2:27PM up 15:59, 1 users, load averages: 0.44, 0.55, 0.51

Who is on the server? ec2-user pts/0 Dec 17 14:26 (pool-123-45-678-91.state.fios.verizon.net)

Recent logs ec2-user pts/0 Dec 17 14:26 pool-123-45-678-91.state Sun Dec 17 14:26 still logged in pool-123-45-678-91.state Sun Dec 17 05:12 - 05:13 (00:01) pool-123-45-678-91.state Sun Dec 17 02:45 - 05:12 (02:26) ec2-user pts/0 pool-123-45-678-91.state Sat Dec 16 22:28 - 02:16 (03:47) boot time Sat Dec 16 22:28 shutdown time Sat Dec 16 22:27 ec2-user pts/0 pool-123-45-678-91.state Sat Dec 16 21:48 - shutdown (00:38) ec2-user pool-123-45-678-91.state Fri Dec 15 23:45 - 23:45 (00:00) ec2-user pts/0 pool-123-45-678-91.state Fri Dec 15 23:23 - 23:23 (00:00) ec2-user pts/0 pool-123-45-678-91.state Fri Dec 15 23:23 - 23:23 (00:00) boot time Fri Dec 15 23:23 boot time Fri Dec 15 23:21 utx.log begins Fri Dec 15 23:21:59 UTC 2017

What is the best way to format the outputs so that is looks like the outputs on the command lines?

Lgalan90

Posted 2017-12-17T03:03:05.650

Reputation: 13

Could you include received message in "raw format"? [Remove Received: headers] – AnFi – 2017-12-17T09:00:08.750

@AnFi Good morning! I have added the raw format message, let me know if you need anything else. – Lgalan90 – 2017-12-17T14:39:15.170

Answers

1

The variable $last is being echoed as individual words - quote it ("$last") to preserve newlines.

varro

Posted 2017-12-17T03:03:05.650

Reputation: 179

Excellent! I had set it up like so: echo "Recent logs: " "$(last)" . Now it looks fine! However, I also have this as file I can view through the browser and is wrapped with HTML tags, how would i get it to format within the <body> tags? – Lgalan90 – 2017-12-17T19:00:22.290

I'm not an expert on HTML, but I think specifying surrounding the text with <pre> </pre> tags should do it. – varro – 2017-12-17T19:35:50.170

You're the man!! @varro It looks much better now, tyvm for you help and have a happy holidays and happy new years! – Lgalan90 – 2017-12-17T21:39:28.043

I'm glad I was able to help. If you are satisfied with the answer, please upvote. And I hope you have a happy holiday season too! – varro – 2017-12-17T23:24:00.283