Shell Script To E-Mail


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script To E-Mail
# 1  
Old 11-15-2012
Hammer & Screwdriver Shell Script To E-Mail

Hi All,

I'm having some problems with my shell script. When running the script I get the following errors:

line 101: <html>: command not found
line 105: /dumpfile.txt: No such file or directory

The file dumpfile.txt does exist and I have double and tripple checked this. I'm not sure what I am doign wrong with the HTML tag part here as I want it to attach an html file. Any advice please?

Code:
#!/bin/bash
# ===================================================================== #
#     OTRS BACKUP SCRIPT WITH SYSTEM INFORMATION    #
#       SCRIPT CREATED 15/11/2012      #
#      SCRIPT LAST MODIFIED 15/11/2012      #
# ===================================================================== #

# ===================================================================== #
#      VARIABLES THAT CAN BE USED IN THE SCRIPT     #
# ===================================================================== #

# CREATES A TITLE VARIABLE
TITLE="OTRS System Status"
# GATHERS THE SYSTEM TIMESTAMP INFORMATION
SYS_DATE=$(date)
# SPECIFY THE CONTENT OF THE E-MAIL TO BE SENT
MAILCONTENT="files"
# SETS THE MAXIMUM SIZE OF THE E-MAIL 4000 = +/- 5MB
MAXATTSIZE="4000"
# E-MAIL ADDRESS WHERE THE E-MAIL WILL BE SENT
MAILADDR="email@domain.com"
# DEFINE THE E-MAIL BODY
SUBJECTFILE=$BACKUPDIR/dumpfile.txt

# ===================================================================== #
#        FUNCTIONS         #
#      STUBBING IS USED AS A TEMPORY PLACE HOLDER FOR FUTURE CODE       #
# ===================================================================== #

function mysql_dump
{
 echo "<pre>"
 mysqldump db1 > db1.sql --user=user --password=password &&  mysqldump db2 > db2.sql --user=user --password=password
 echo "<pre>"
}
# ---- END OF MYSQL DUMP ---- #

function drive_space
{
    echo "<h2>Filesystem Space</h2>"
    echo "<pre>"
    df -h
    echo "</pre>"
}
# ---- END OF DRIVE SPACE ---- #

function backup_size
{
    echo "<h2>Backup Size</h2>"
    echo "<pre>"
    du -h *.sql
    echo "</pre>"
}
# ---- END OF BACKUP SIZE ---- #

function additional_function
{
    # TEMPORARY FUNCTION STUB
    echo "function additional_function"
}
# ---- END OF ADDITIONAL FUNCTIONS ---- #

# ===================================================================== #
#     EXECUTING MYSQL_DUMP FUNCTION       #
# ===================================================================== #
$(mysql_dump)

# ===================================================================== #
#     GATHERS AND DISPLAYS THE STATUS OF THE SYSTEM   #
# ===================================================================== #
function attachment
{
cat <<- _OTRSSTATUS_
  <html>
  <head>
  <title>$TITLE</title>
  </head>
  
  <body>
  <h1>$TITLE</h1>
  <p></p>
  $(drive_space)
  $(backup_size)
  <body>
  </html>
_OTRSSTATUS_
}

# ===================================================================== #
#    GATHERS INFORMATION AND SEND MESSAGE ATTACHMENT    #
# ===================================================================== #
$(attachment)
if [ "$MAILCONTENT" = "files" ]
 then
 mutt -s "System Status $DATE" -a $(attachment) -- $MAILADDR < $SUBJECTFILE
fi

exit 0

# 2  
Old 11-15-2012
Hi, you cannot use command substitution like this:
Code:
$(mysql_dump)

or
Code:
$(attachment)

This will try to execute the result of the substitution...
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-15-2012
Ok, can you maybe help me get it working? I have manage to fix the first few errors and now I get the following:

Can't stat <html>: No such file or directory
<html>: unable to attach file.
# 4  
Old 11-15-2012
Try using double quotes around variable references, for example "$(attachment)"
# 5  
Old 11-15-2012
Quote:
Originally Posted by Scrutinizer
Try using double quotes around variable references, for example "$(attachment)"
I tried this, and now it prints the output of the shell script. However it still doesn't attach and e-mail the file.

At the start of the print screen I get
Code:
Can't Stat

At the end I get
Code:
</html>: unable to attach file.

---------- Post updated at 03:34 PM ---------- Previous update was at 02:02 PM ----------

I have managed to get it to actually attach the file. But without and contents.

Code:
#!/bin/bash
# ===================================================================== #
# OTRS BACKUP SCRIPT WITH SYSTEM INFORMATION #
# SCRIPT CREATED 15/11/2012 #
# SCRIPT LAST MODIFIED 15/11/2012 #
# ===================================================================== #
 
# ===================================================================== #
# VARIABLES THAT CAN BE USED IN THE SCRIPT #
# ===================================================================== #
# DIRECTORY WHERE FILES ARE STORED
BACKUPDIR="/mnt/data6/reports"
# CREATES A TITLE VARIABLE
TITLE="System Status"
# SPECIFY THE CONTENT OF THE E-MAIL TO BE SENT
MAILCONTENT="files"
# SETS THE MAXIMUM SIZE OF THE E-MAIL 4000 = +/- 5MB
MAXATTSIZE="4000"
# E-MAIL ADDRESS WHERE THE E-MAIL WILL BE SENT
MAILADDR=mymail@domain.com
# DEFINE THE E-MAIL BODY
SUBJECTFILE=$BACKUPDIR/sqldump.txt
# CREATE THE FILE TO ATTACH
ATTACHMENT=$BACKUPDIR/Status.html
 
# ===================================================================== #
# FUNCTIONS #
# STUBBING IS USED AS A TEMPORY PLACE HOLDER FOR FUTURE CODE #
# ===================================================================== #
 
 
 
 
function drive_space
{
echo "<h2>Filesystem Space</h2>"
df -h
}
# ---- END OF DRIVE SPACE ---- #
 
function backup_size
{
echo "<h2>Backup Size</h2>"
du -h sqldump.sh
}
# ---- END OF BACKUP SIZE ---- #
 
function additional_function
{
# TEMPORARY FUNCTION STUB
echo "function additional_function"
}
# ---- END OF ADDITIONAL FUNCTIONS ---- #
 
 
 
 
# ===================================================================== #
# GATHERS AND DISPLAYS THE STATUS OF THE SYSTEM #
# ===================================================================== #
function ATTACHMENT
{
cat <<-_Status_
<html>
<head>
<title>$TITLE</title>
</head>

<body>
<h1>$TITLE</h1>
<p></p>
$(drive_space)
$(backup_size)
<body>
</html>
_Status_ > $ATTACHMENT
}
# ===================================================================== #
# GATHERS INFORMATION AND SEND MESSAGE ATTACHMENT #
# ===================================================================== #
 
 
if [ "$MAILCONTENT" = "files" ]
then
mutt -s "System Status $DATE" -a $ATTACHMENT -- $MAILADDR < $SUBJECTFILE
fi
 
exit 0
 

# 6  
Old 11-15-2012
Hi, you need redirect on the first line:

Code:
cat <<-_Status_ > file

You would also need to call the function..
# 7  
Old 11-16-2012
Hammer & Screwdriver

I still seem to be having a problem with the attachment actually containing any data. The script runs sending an e-mail with an HTML file attached, however when I open the HTML file it is blank. Below is what my script currently looks like.


Code:
#!/bin/bash
# ===================================================================== #
#      BACKUP SCRIPT WITH SYSTEM INFORMATION    #
#       SCRIPT CREATED 15/11/2012      #
#      SCRIPT LAST MODIFIED 16/11/2012      #
# ===================================================================== #
 
# ===================================================================== #
#      VARIABLES THAT CAN BE USED IN THE SCRIPT     #
# ===================================================================== #
# DIRECTORY WHERE FILES ARE STORED
BACKUPDIR="/mnt/data6/reports"
# CREATES A TITLE VARIABLE
TITLE=" System Status"
# SPECIFY THE CONTENT OF THE E-MAIL TO BE SENT
MAILCONTENT="files"
# SETS THE MAXIMUM SIZE OF THE E-MAIL 4000 = +/- 5MB
MAXATTSIZE="4000"
# E-MAIL ADDRESS WHERE THE E-MAIL WILL BE SENT
MAILADDR="myemail@mydomain.com"
# DEFINE THE E-MAIL BODY
SUBJECTFILE=$BACKUPDIR/sqldump.txt
# CREATE THE FILE TO ATTACH
ATTACHMENT=$BACKUPDIR/Status.html
 
# ===================================================================== #
#        FUNCTIONS         #
#      STUBBING IS USED AS A TEMPORY PLACE HOLDER FOR FUTURE CODE       #
# ===================================================================== #
 
 
function drive_space
{
    echo "<h2>Filesystem Space</h2>"
    df -h
}
# ---- END OF DRIVE SPACE ---- #
 
function backup_size
{
    echo "<h2>Backup Size</h2>"
    du -h _sqldump.sh
}
# ---- END OF BACKUP SIZE ---- #
 
function additional_function
{
    # TEMPORARY FUNCTION STUB
    echo "function additional_function"
}
# ---- END OF ADDITIONAL FUNCTIONS ---- #
 
 
# ===================================================================== #
#     GATHERS AND DISPLAYS THE STATUS OF THE SYSTEM   #
# ===================================================================== #
function ATTACHMENT
{
"cat <<-_Status_
  <html>
  <head>
  <title>$TITLE</title>
  </head>
 
  <body>
  <h1>$TITLE</h1>
  <p></p>
  $(drive_space)
  $(backup_size)
  <body>
  </html>
_Status_"> $ATTACHMENT
}
# ===================================================================== #
#    GATHERS INFORMATION AND SEND MESSAGE ATTACHMENT    #
# ===================================================================== #
 
if [ "$MAILCONTENT" = "files" ]
 then
 mutt -s " System Status $DATE" -a $ATTACHMENT -- $MAILADDR < $SUBJECTFILE
fi
 
exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A shell script to run a script which don't get terminated and send a pattern from the output by mail

Hi Guys, I am very new to shell script and I need your help here to write a script. Actually, I have a script abc.sh which don't get terminated itself. So I need to design a script to run this script, save the output to a file, search for a given string in the output and if it exists send those... (11 Replies)
Discussion started by: Sambit Sahu
11 Replies

2. Shell Programming and Scripting

To send a mail through shell script

I want to send a mail through shell script,If it is possible Please give me a code. mail id : upload.xxx@example.com (8 Replies)
Discussion started by: kannansoft1985
8 Replies

3. Shell Programming and Scripting

Sending Mail via shell script

I am in need of a script that will send out emails while going through a NAT. What I have that works is as follows: display_remote_IP | sort | uniq | while read i do FOUND=0 for IP in `echo $ACCEPTABLEIP` do if ; then FOUND=1... (2 Replies)
Discussion started by: ldapguy
2 Replies

4. Shell Programming and Scripting

Sending mail from shell script

Hello All, I m trying to send mail from my unix script, I have used the below command mailx -s 'hi' email address < temp.txt It is not giving me any error,but I couldn't receive the mail Can you please help me. Many Thanks, Pragyan (6 Replies)
Discussion started by: prarat
6 Replies

5. UNIX for Dummies Questions & Answers

How to send e-mail from shell script ( C shell )?

Hi , How to send e-mail from shell script ( C shell ) . Mailx command is not working ( It didn't giving error also ). Please help me (2 Replies)
Discussion started by: arukuku
2 Replies

6. Shell Programming and Scripting

Mail shell script

Hi All, I want to send mail to multiple users.. Currently I am using below script cat $STATUS_FILE|mailx -s "$SUBJECT" -r xxx@yyy.com $MAILTO How can i give cc or bcc to in this script Please help me thanks in advance Regards RG (3 Replies)
Discussion started by: rgumm
3 Replies

7. Shell Programming and Scripting

sending a mail using shell script

Please help me in writing the script for sending an attachment through email.(For example my text file name is :abc.txt and it is in directory d:/abc) (1 Reply)
Discussion started by: anitha126
1 Replies

8. Shell Programming and Scripting

Shell script to send a mail

Hi , I need to prepare a script which will check my database with specific to particluar table. If the row count exceeds a certain limit, i need to send a mail to a set of Recipients. Being new to unix, please guide me to complete this task. Advance thanks, Sekar. (4 Replies)
Discussion started by: Sekar1
4 Replies

9. Shell Programming and Scripting

shell script to send a mail

Hi, I need a shell script which runs in the backround for all the 24 hours and send a mail to us regarding the output of the prstat command when the load average increase above certain percent. kindly help me on this...... (1 Reply)
Discussion started by: jayaramanit
1 Replies

10. Shell Programming and Scripting

Mail command in a shell script

i wanted to write a shell script that can automatically read unread mails and copy each one into a different file. I was trying to use the MAIL command to do it, but this command requires user input inbetween, I was wondering how that could be achieved in a shell script. Can sumone please suggest... (0 Replies)
Discussion started by: garric
0 Replies
Login or Register to Ask a Question