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

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

Thanks,
Karthik

To get the output of a command in a variable, you need to use backquotes instead of quotes.

var=`some_command`

but it is better to use this:

var=$(some_command)

Also it is best to quote variable expansions:

cat "$FILE"
echo "$content"

In addition to what Scrutinizer has already said, the command:

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:

content=$(cat "$File")
echo "$content" | mail -s "Subject " "myname@company.com"

with:

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

Hi,

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

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

Thanks.
Karthik

  • What is the content of the variable File ?
  • What is the result if you use cat -- "$File" ?

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

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?

As I said before, using cat to send the contents of your chosen file to the mail utility is grossly inefficient and in this case is generating an error that could have been easily avoided if you had followed my suggestion in post #3 in this thread.

In addition to that there is nothing in your find command that has anything to do with finding a file with today's date.

If you would give us clear details about the names and locations of the files from which you want to select today's file(s), how you determine that a file is today's file (Is it part of the name of the file? Is it the last modification timestamp of the file? Is it the last access timestamp of the file? ...), we might be able to make some meaningful suggestions. If you keep us in the dark, all we can do is treat the symptoms indicated by the error messages you are describing to us. Fixing those symptoms might or might not lead you to something that will do what you are trying to do.

Please help us help you.