Question

Snapshot List outputs in file and email as single line

Posted March 23, 2020 164 views
DigitalOcean API and CLI (doctl)

I’m looking to develop a script where the Snapshot list is emailed to someone.

When I run doctl compute droplet snapshots $dropletid, I get the following:

ID          Name                                     Type        Distribution    Slug    Public    Min Disk
61003635    test-droplet_2020-03-22-Sun@17:34-CDT    snapshot    Ubuntu                  false     25
61004052    test-droplet_2020-03-22-Sun@17:46-CDT    snapshot    Ubuntu                  false     25
61004228    test-droplet_2020-03-22-Sun@17:52-CDT    snapshot    Ubuntu                  false     25
61005132    test-droplet_2020-03-22-Sun@18:07-CDT    snapshot    Ubuntu                  false     25
61005281    test-droplet_2020-03-22-Sun@18:11-CDT    snapshot    Ubuntu                  false     25
61007940    test-droplet_2020-03-22-Sun@19:04-CDT    snapshot    Ubuntu                  false     25

However, when I echo that to an email, with this sudo /snap/bin/doctl compute droplet snapshots $dropletid >> $email_notification, I get the following in the email:

ID Name Type Distribution Slug Public Min Disk 61003635 test-droplet_2020-03-22-Sun@17:34-CDT snapshot Ubuntu false 25 61004052 test-droplet_2020-03-22-Sun@17:46-CDT snapshot Ubuntu false 25 61004228 test-droplet_2020-03-22-Sun@17:52-CDT snapshot Ubuntu false 25 61005132 test-droplet_2020-03-22-Sun@18:07-CDT snapshot Ubuntu false 25 61005281 test-droplet_2020-03-22-Sun@18:11-CDT snapshot Ubuntu false 25 61007940 test-droplet_2020-03-22-Sun@19:04-CDT snapshot Ubuntu false 25 61008918 test-droplet_2020-03-22-Sun@19:15-CDT snapshot Ubuntu false 25

Instead of the output being on one line, I want the output to be on as many lines as being requested.

Is there something I’m missing?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi there @aaronmweiss,

What I usually would do in this case is save the output in a file, then with send mail you could proceed that file like this:

  • sendmail email@yourdomain.com < output_file.txt

You can set your subject as the first line of the file:

Subject: Your mail subject

To save the output to a file it would look something like this:

#!/bin/bash

echo 'Subject: Your mail subject' > ~/output_file.txt

doctl compute droplet snapshots $dropletid >> ~/output_file.txt

sendmail email@yourdomain.com < ~/output_file.txt

To make things more secure rather than having a specific output file, I would recommend using the mktemp command:

#!/bin/bash

temp_output=$(mktemp /tmp/temp-output.XXXXXX)

echo 'Subject: Your mail subject' > ${temp_output}

doctl compute droplet snapshots $dropletid >> ${temp_output}

sendmail email@yourdomain.com < ${temp_output}

rm -f ${temp_output}

That way you would always have a different temp file.

Hope that this helps!
Regards,
Bobby

Submit an Answer