LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2014, 06:51 PM   #1
hotdang
LQ Newbie
 
Registered: Mar 2014
Posts: 2

Rep: Reputation: Disabled
Bash Script send one email after a loop


I created a bash script that gets a file formats it, sends an email to the user(s), however, for whatever reason, it will send an email per line with all of the lines in it, so essentially the user will get 10 duplicate emails with the same 10 results. I've modified the script to put the results of each user into a file with the user's email address, but I'm not sure how to limit the script to send only one email on however many lines there is for each user. Please see what I have, I'd appreciate any suggestions.

Code:
printf "${EMAIL%%@*},\n\n$MESSAGE\n\n$DETAIL" | mailx -r admin@admin.com -s "test" "$EMAIL"
        DETAIL=''
done
Is there any way I can have some sort of function or something that will only allow to send one email? I'm a bit stumped as to how to fix it.
 
Old 03-13-2014, 08:37 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I think I must have missed something

If you have the mailx in a loop, surely you would understand that it will send how ever many emails the loop performs iterations?

If you only want to send one email, don't put in a loop.

Like I said at the start, I am a little lost, would you mind explaining a little further?
 
Old 03-13-2014, 08:58 PM   #3
hotdang
LQ Newbie
 
Registered: Mar 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
Sorry for the confusion. The problem is...I have a list of user IDs and corresponding data. The script has to loop through the data, and essentially send out an email with the list of items per user. However, right now, when I run the script, instead of getting 1 email with say 50 items. It's sending each user 50 emails with 50 items, essentially a lot of duplicate emails. I'd like to know if there is a way for me to not send tons of duplicate emails, instead 1 email with the 50 items. Hope that clarifies.
 
Old 03-14-2014, 01:26 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Ok ... So I follow a little better So my next question would be, where are the items coming from?

My initial thought is to place the pipe to mailx on the end of the loop creating the items:
Code:
for EMAIL in email1 email2 ...
do
    while / for ...
    do
        <create / get list of items to be sent>
    done | mailx -r admin@admin.com -s "test" "$EMAIL"
done
 
Old 03-14-2014, 01:31 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Of course it would be a lot easier if you posted the script ...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script to Continuously Ping Server and Send Email schaefer76 Linux - Newbie 4 05-07-2010 09:54 AM
Bash script to check if process is running and send email depam Linux - Newbie 2 04-08-2009 12:11 AM
bash script in background - awk and send email noir911 Programming 4 03-22-2009 09:46 PM
bash script to send email when internet cap is reached aeby Linux - Server 7 08-22-2007 07:46 AM
Write a script to send an email from bash kpelczar Linux - Software 5 02-09-2005 04:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration