Send file content in mail


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Send file content in mail
# 1  
Old 09-18-2016
Send file content in mail

Hi .

I am new to scripting.I am trying to mail the recent file contents but not the below script which i wrote is not working.Please guide me in finishing the script

Code:
File='find /Directory path/*.fnr | tail -1'
content=' cat $File'
echo $content | mail -s "Subject " "myname@company.com"


Thanks,
Karthik

Last edited by Scrutinizer; 09-18-2016 at 02:28 AM.. Reason: code tags
# 2  
Old 09-18-2016
To get the output of a command in a variable, you need to use backquotes instead of quotes.
Code:
var=`some_command`

but it is better to use this:
Code:
var=$(some_command)

Also it is best to quote variable expansions:
Code:
cat "$FILE"

Code:
echo "$content"

# 3  
Old 09-18-2016
In addition to what Scrutinizer has already said, the command:
Code:
File=$(find /Directory path/*.fnr | tail -1)

is very strange. Unless the pathname matching pattern path/*.fnr fails to match any pathnames, the search of the file hierarchy rooted in /Directory doesn't do anything but slow down finding the last file in the file hierarchies rooted in the pathnames matched by path/*.fnr. Note also that unless the pathnames matched by path/*.fnr are non-directory files, the last pathname printed by find from this command will be random (i.e., not necessarily in alphanumeric order, not time created order; just random). What is the intent in picking a random file from the output of find?

And, once you have selected a file, copying the contents of file into a variable with the sole purpose of copying the text into a mail message wastes memory, time, CPU bandwidth, and I/O bandwidth. It would be MUCH more efficient to replace:
Code:
content=$(cat "$File")
echo "$content" | mail -s "Subject " "myname@company.com"

with:
Code:
mail -s "Subject " "myname@company.com" < "$File"

# 4  
Old 09-18-2016
Hi,

I have changed the code as you mentioned but the cat command is not executing content=$(cat "$File") getting below error.

Code:
cat: invalid option -- 'r'
Try `cat --help' for more information.

Thanks.
Karthik


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

Last edited by RudiC; 09-18-2016 at 09:31 AM.. Reason: Added CODE tags.
# 5  
Old 09-18-2016
  • What is the content of the variable File ?
  • What is the result if you use cat -- "$File"?
# 6  
Old 09-18-2016
Hi,

I just want to cat the contents of the file *.fnr and mail the content in mail.

My requirement :

Find the file name in today date.

cat the log file and to send the log in mail.

It is log file which show the backup status of database.

Thanks,
Karthik
# 7  
Old 09-18-2016
If you can't find out yourself what the error msg means, and if you want meaningful help, wouldn't it be wise to answer questions needed to track down the details of your problem?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Client was not authenticated to send anonymous mail during MAIL FROM (in reply to MAIL FROM comm

I am having trouble getting mail to work on a red hat server. At first I was getting this message. Diagnostic-Code: X-Postfix; delivery temporarily suspended: connect to :25: Connection refused Then added the port to my firewall. Then I temporarily turned off selinux. I then copied this file... (1 Reply)
Discussion started by: cokedude
1 Replies

2. Shell Programming and Scripting

Shell script to monitor new file in a directory and mail the file content

Hi I am looking for a help in designing a bash script on linux which can do below:- 1) Look in a specific directory for any new files 2) Mail the content of the new file Appreciate any help Regards Neha (5 Replies)
Discussion started by: neha0785
5 Replies

3. Shell Programming and Scripting

How to copy mail content in a file in Unix

Hi Guys I want to write a script which search mail with subject line and after that I want the mail content in a file please help guys. Thanks Atul Singh (3 Replies)
Discussion started by: atul9806
3 Replies

4. UNIX for Advanced & Expert Users

need to configure mail setting to send mail to outlook mail server

i have sun machines having solaris 9 & 10 OS . Now i need to send mail from the machines to my outlook account . I have the ip adress of OUTLOOK mail server. Now what are the setting i need to do in solaris machines so that i can use mailx or sendmail. actually i am trying to automate the high... (2 Replies)
Discussion started by: amitranjansahu
2 Replies

5. UNIX for Dummies Questions & Answers

How to send html file in a mail not as an attachment but it should display in the mail in table for

Hi The below script working when we are sending the html as attachment can u please guide how to send thesmae data in table form direct in the mail and not in mail attachment . cat Employee.sql SET VERIFY OFF SET PAGESIZE 200 SET MARKUP HTML ON SPOOL ON PREFORMAT OFF ENTMAP ON - HEAD... (0 Replies)
Discussion started by: mani_isha
0 Replies

6. UNIX for Dummies Questions & Answers

How to send file in mail

Hi I want to send out a file that generate 1st of each month that have formate like this 11012008_experience_rate_log.txt Now I have setup a cronjob that usually sent this file in mail like cat /data02/transfer/*_experience_rate_log.txt | mail -s 'PICS EXP RATE Logs' lger@bd.com So how... (3 Replies)
Discussion started by: vishalpatel03
3 Replies

7. UNIX for Dummies Questions & Answers

To send an email with the body content extracted from a file

Hi, I have been trying to shoot an email with the email body to be obtained from a file. Can someone please help me with it.. I have been trying to use the MAILX commad for the same. mailx -s "test email" -r sender@test.com < file.txt but it sends the file as an attachment,while i... (3 Replies)
Discussion started by: rohit.shetty84
3 Replies

8. Shell Programming and Scripting

Pull E-mail address from file, send e-mail

Hello, I am new to perl and need to create a script that will read a file and pull a name from the file and send e-mail. How can I use the following awk statement in a perl script? grep UNIXadmins /root/mail.conf | awk '{ print $2}' and use the output to send a e-mail. Any help would... (1 Reply)
Discussion started by: DC Heard
1 Replies

9. UNIX for Dummies Questions & Answers

To send a mail with a file attached

Hi, Can anyone please tell me how to send a mail to a person in unix with a file attached. The file need to be zipped and then i want to send the mail to a person. I know that we can able to send mail from unix using the command mail mailaddress to send a mail to a person with the... (5 Replies)
Discussion started by: samudha
5 Replies

10. UNIX for Dummies Questions & Answers

Send a mail with an attachement of a file

I wanted to try sending a mail with an attachement at command prompt in unix. Some one please advise that how we can do it ? Thanks, :) (2 Replies)
Discussion started by: gaddeg
2 Replies
Login or Register to Ask a Question