Login or Register to Ask a Question and Join Our Community


UNIX script for consolidated email


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX script for consolidated email
# 1  
Old 12-05-2016
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 Smilie

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

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


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-05-2016 at 11:50 AM.. Reason: Added CODE tags.
# 2  
Old 12-05-2016
How about shifting the mail command after the loop?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-05-2016
That is because the mail command is inside the loop. Also you can redirect the loop output to the file

You could try:
Code:
#!/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:
Code:
#!/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


Last edited by Scrutinizer; 12-05-2016 at 11:50 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 12-05-2016
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 ?
# 5  
Old 12-05-2016
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Executing of UNIX script using email

Dear Unix Leads, can you please let me know is it possible to execute a shell script in UNIX machine sending an email from outlook or gmail ? or is it possible to generate a token file in UNIX by sending email which we can indirectly use to trigger script your response on this is highly... (5 Replies)
Discussion started by: mirwasim
5 Replies

2. Shell Programming and Scripting

UNIX script email to outlook report format

I have a ksh script that emails a report to outlook from a Unix Solaris. On server the report formatted perfectly. However, the email version the format is not. On the server report looks like this: TN Region Old SPiAD Server Order/Req Local Status NAAC Status Request Date New... (1 Reply)
Discussion started by: mrn6430
1 Replies

3. Shell Programming and Scripting

Automated script once user send email to UNIX server

is there any possibility to trigger a script. once the user send email to the unix server box with specific subject line. My script will search for the specific word in the unix email and run the shell script if the specific keyword is sent in the email to the unix email box. so that it can... (3 Replies)
Discussion started by: ramkumar15
3 Replies

4. Shell Programming and Scripting

Consolidated output in shell script

Hi Gurus, I have a shell script which connects to mulitple DBs and run some queries and output will be written to log file.I need the consolidated output (html format will be better)from the logfile.see below for more details about my requirement. Actual output: DB_NAME TABLE_OWNER ... (9 Replies)
Discussion started by: navsan420
9 Replies

5. Shell Programming and Scripting

Unix Script to email grepped results

I am following the tread number 81556 to grep a file and send the results in email. #/bin/bash /bin/grep -i 'invite sip' /var/log/asterisk/full if echo "found" | mail -s 'invite sip' mail@gmail.com When I just build it to grep and mail, it works fine. However, the if statement causes... (4 Replies)
Discussion started by: klysdale
4 Replies

6. Shell Programming and Scripting

Unix Shell Script to automate email alert

Hi all, I have a task on my plate which is of high priority. I need an automated email alert that checks FTP notices subdirectory on a daily basis and forwards any word files to a group of people. This word files gets created whenever there is an issue with FTP connectivity. Please help...... (1 Reply)
Discussion started by: stunnerz_84
1 Replies

7. UNIX for Advanced & Expert Users

send a message through email to 5 people using unix script.

Hi All, I want to send a message through email to 5 people in unix script. Please let me know, how to do it.Please do reply it is urgent. Thanks, Mary. (3 Replies)
Discussion started by: MARY76
3 Replies

8. Shell Programming and Scripting

send a message through email to 5 people using unix script?

Hi All, I want to send a message through email to 5 people in unix script. Please let me know, how to do it.Please do reply it is urgent.How to do it?Please reply.thanks! Thanks, Mary. (2 Replies)
Discussion started by: MARY76
2 Replies

9. Shell Programming and Scripting

sending email from KSH unix script.

Hi Need guidance on including code to mail a couple of files atached with some subject and mail body !!.. Thanks in advance (3 Replies)
Discussion started by: rosh0623
3 Replies

10. UNIX for Dummies Questions & Answers

need a unix script to let me know by email or pager when the filesystem is 80% full.

Does anyone have a script to check disk space usage. My backup directory keeps filling up with archivelog files and I need a script to let me know by email or pager when the filesystem is 80% full. Thank you! (1 Reply)
Discussion started by: jzjy0r
1 Replies
Login or Register to Ask a Question

Featured Tech Videos