Read a file, add some text and send an email


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read a file, add some text and send an email
# 1  
Old 04-12-2013
Read a file, add some text and send an email

Hi,

If I am asking this question, you must have already figured out , that I am new to Unix, so here it goes

I was trying to read a file, add some user defined content to it and send out an email , I did find out a way to achieve this, but looking at the code, it looks a bit crude to me, can you guys point out a more efficient way of writing this?

Here is the my Input file id_list.txt
Code:
Bob
Marcus
Adam

And this is what I expect to be sent in the email
Code:
This is a test email 

This is an email to notify you that the following ID's are going to expire in 1 week 

Please change the password for "Bob" to avoid being locked out.

Please change the password for "Marcus" to avoid being locked out.

Please Change the Password for "Adam" to avoid being locked out.

And this is what I wrote
Code:
EMAIL_TEXT1="This is a test email"
EMAIL_TEXT2="This is an email to notify you that the following ID's are going to expire in 1 week"
echo 
{
echo $EMAIL_TEXT1 $EMAIL_TEXT2
{
while read userID;
 do 
 { echo -n " Please change the password for " && echo -n \"$userID\" && echo " to avoid being locked out.\n"; }
done < id_list.txt
} }> final_op.txt
mutt -s "Test Email" "karthik@xyz.com" < final_op.txt

Can someone point out a better way to write this? Also can you also explain, instead of writing the file to the final_op.txt , if i pipe it to the mutt command, the email comes up in a non formatted way, that is the text inside the while loop comes up in a single line. But if i seperate it out it comes as I have mentioned in the expected output .

Thanks,
Karthik
# 2  
Old 04-12-2013
Quote:
Originally Posted by karthikbhuvana
Hi,

If I am asking this question, you must have already figured out , that I am new to Unix, so here it goes

I was trying to read a file, add some user defined content to it and send out an email , I did find out a way to achieve this, but looking at the code, it looks a bit crude to me, can you guys point out a more efficient way of writing this?

Here is the my Input file id_list.txt
Code:
Bob
Marcus
Adam

And this is what I expect to be sent in the email
Code:
This is a test email 

This is an email to notify you that the following ID's are going to expire in 1 week 

Please change the password for "Bob" to avoid being locked out.

Please change the password for "Marcus" to avoid being locked out.

Please Change the Password for "Adam" to avoid being locked out.

And this is what I wrote
Code:
EMAIL_TEXT1="This is a test email"
EMAIL_TEXT2="This is an email to notify you that the following ID's are going to expire in 1 week"
echo 
{
echo $EMAIL_TEXT1 $EMAIL_TEXT2
{
while read userID;
 do 
 { echo -n " Please change the password for " && echo -n \"$userID\" && echo " to avoid being locked out.\n"; }
done < id_list.txt
} }> final_op.txt
mutt -s "Test Email" "karthik@xyz.com" < final_op.txt

Can someone point out a better way to write this? Also can you also explain, instead of writing the file to the final_op.txt , if i pipe it to the mutt command, the email comes up in a non formatted way, that is the text inside the while loop comes up in a single line. But if i seperate it out it comes as I have mentioned in the expected output .

Thanks,
Karthik
Try:
Code:
EMAIL_TEXT1='This is a test email'
EMAIL_TEXT2="This is an email to notify you that the following ID's are going to expire in 1 week" 
( printf "%s\n\n%s\n" "$EMAIL_TEXT1" "$EMAIL_TEXT2"
  while read userID
  do    printf "\nPlease change the password for \"%s\" to avoid being locked out.\n" "$userID"
  done < id_list.txt
) | mutt -s 'Test Email' 'karthik@xyz.com'

# 3  
Old 04-13-2013
Quote:
the text inside the while loop comes up in a single line.
But if i seperate it out it comes as I have mentioned in
the expected output .
Could you clarify how you want the output (the mail message) not to be a single line?
# 4  
Old 04-13-2013
Quote:
Originally Posted by hanson44
Could you clarify how you want the output (the mail message) not to be a single line?
Hi hanson44,
Karthi provided sample output showing exactly the format of the message wanted.

However, Karthi did not say what OS is being used. And, has been stated in this forum dozens (if not hundreds) of times before, you should never use echo if any operand contains a backslash character nor if any argument starts with a minus sign if you want to use your script on different systems. The replacement script I provided replaces the non-portable echo commands with portable printf commands and should produce mail messages in the stated format without the need for a temp file.

As far as why Karthi got a single line of output, I don't know. Even on a system that treats -n as a request to skip the trailing newline and does not translate \n to a newline, there still should have been two lines of output rather than one.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell Script to read hive table and send email

HI Team, I am working on reading hive table and send email in email body using shell script, can you please help on fixing the errors: I have 6 columns in my hive table and trying to send the email in the mail body. below script: #!/bin/bash hive -e 'SELECT count(*) from db.table' >... (4 Replies)
Discussion started by: Mi4304
4 Replies

2. UNIX for Beginners Questions & Answers

Merge Text with space into once cell and send email out

Please help!! This code works perfect but the only problem I am having is that is treats eg SQL Developer as separate cell/column which makes the formatting bad. I want to put SQL Developer in one cell. I attached a sample of how the output looks like. report.txt USERNAME OSUSER ... (5 Replies)
Discussion started by: lpoolfc
5 Replies

3. Shell Programming and Scripting

Read file and send email

I have a file like this. I need to ues this file to send emails to the appropriate ID/group. For instance in the first line - Subject should be --> "A1.csv - ABC" - Body should be --> File A1.csv has changed. - Email should be sent to A1@xyz.com,A3@xyz.com Lookup.csv: ... (1 Reply)
Discussion started by: vskr72
1 Replies

4. UNIX for Dummies Questions & Answers

send an email from batch file

My batch files run every day, it moves files from servers to my local computer, can I have a confirmation email sent to me from this batch file saying if the job failed or successes? Thank you for your help! (6 Replies)
Discussion started by: idiazza
6 Replies

5. Shell Programming and Scripting

Script to read file size and send email only if size > 0.

Hi Experts, I have a script like $ORACLE_HOME/bin/sqlplus username/password # << ENDSQL set pagesize 0 trim on feedback off verify off echo off newp none timing off set serveroutput on set heading off spool Schemaerrtmp.txt select ' TIMESTAMP COMPUTER NAME ... (5 Replies)
Discussion started by: welldone
5 Replies

6. Cybersecurity

How can you send a file over Ip.NOT EMAIL

So I was just wondering... Is there a way to send a file directly to the computer..... Ive never gotten the answer and I want to know how, I keep running in to a wall :wall:. Why you may ask, well really for security reasons due to the fact that if this is done via a port I want to make sure to... (3 Replies)
Discussion started by: orszhak
3 Replies

7. Shell Programming and Scripting

How to read specific line of text from a Script and send email notification

Hi ! I am a newbie and never officially wrote a shell script before. The requirement for this script is : 1) Read a file called 'bpm.log' and identify if it has a specific text such as 'this is the text'. Its a static value and that is the only text we need to read. 2) If that... (2 Replies)
Discussion started by: atechcorp
2 Replies

8. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

9. UNIX for Dummies Questions & Answers

How to send email attachment as read only in Linux

Hi, Could anyone help me to find soultion for the issue to send email attachment as read only. I can change the file permission in Linux and can send the email attachment from Linux. But my requirment is user can read the attachment but should not modified it . even user save the attachment... (4 Replies)
Discussion started by: calagar
4 Replies

10. Shell Programming and Scripting

script to send a file in email

a file is created on a daily basis in the name xyz_pqr_20071207.dat.i want to send the file as an attachment if the file contains more than 50 records.how can i write a script such that it will transmit the file after the file is created.i want to sed the file to say asdf@xyz.com. please help me... (2 Replies)
Discussion started by: dr46014
2 Replies
Login or Register to Ask a Question