UNIX script for consolidated email

Hi,

I am working on the below script to get a disk usage report in email from multiple unix systems but the problem I am getting first email with 1 system and second email with 2 systems and finally third email with all 3 systems :slight_smile:

Can someone please let me know how I can get single email out from this script.

#!/bin/sh
rm report.txt
for i in system1 system2 system3 
do
ssh $i df -h | sed -n 9,15p >> report.txt
mail -s "system1 system2 system3  Usage Report" abc@xyz < report.txt
done

How about shifting the mail command after the loop?

1 Like

That is because the mail command is inside the loop. Also you can redirect the loop output to the file

You could try:

#!/bin/sh
for i in system1 system2 system3 
do
  ssh $i df -h | sed -n 9,15p 
done > report.txt
mail -s "system1 system2 system3  Usage Report" abc@xyz < report.txt

or if you do not need the intermediate file, try redirecting the loop output through a pipe to the mail process:

#!/bin/sh
for i in system1 system2 system3 
do
  ssh $i df -h | sed -n 9,15p 
done |
mail -s "system1 system2 system3  Usage Report" abc@xyz
1 Like

And finally the df -h output interms of spacing is not the same as the actual df-h command on these systems..is there any option I can introduce to make sure the display is properly aligned ?

Each df command will align its output. Unless the sizes of all of the fields being output by the various df commands are the same, the output from the commands will not be aligned with each other.

And you haven't bothered to tell us what operating system and shell you're using, so we don't know what features might be available on your system to force the output of the various systems from which you're retrieving data to be aligned. Some systems have an align utility; some don't.

If your system doesn't have an align utility and you know maximum field widths for each of the fields that will work every time you run your script, you can just use printf with an appropriate format string to align fields. Or, if the sizes vary from run to run, you can write an awk script to determine maximum widths and print aligned output.