0

I want to use a bash script to run a backup and then send me an email using ssmtp. I am trying to make a generic script that I can reuse across different Linux machines. Below is a snippet of the script where I use sed to replace some placeholder text with $HOSTNAME and $DATE variables. For some reason when it's run, I get the email but instead of inserting the hostname and date, it wipes out the placeholders. Can someone point out what I'm doing wrong?

Here's the section of my script:

sed -i -e "s/HNAME/$HOSTNAME/g" backup_notification
sed -i -e "s/DATETIME/$date/g" backup_notification

ssmtp [email protected] < backup_notification

Here's the text file of the notification (HNAME and DATETIME are the placeholders I want sed to replace with system variable information):

To: [email protected]
From: [email protected]
Subject: Backup completed

This confirms that backup is completed for HNAME on DATETIME.

And here's what the text of the email has (notice that the HNAME and DATETIME placeholders were wiped or replaced with nil.):

-----Original Message-----
From: root [mailto:[email protected]] 
Sent: Thursday, March 10, 2016 1:15 PM
To: Me
Subject: Backup completed

This confirms that the backup is completed for has been renewed on .

TIA for any help :)!

2 Answers 2

1

$HOSTNAME and $DATE are not defined as variables by default. Instead, use $(command) to insert the output (stdout) from command into the string.

For example, in your case, replace $HOSTNAME with you want to use $(hostname) and $(date):

sed -i -e "s/HNAME/$(hostname)/g" backup_notification
sed -i -e "s/DATETIME/$(date)/g" backup_notification

Also, the -i flag on sed will edit your backup_notification template, as pointed out by @user568271. If you don't want that, you might want to read from it and edit it with a pipe:

cat backup_notification | sed -e "s/HNAME/$(hostname)/g" | sed -e "s/DATETIME/$(date)/g" | ssmtp [email protected]
0

Isn't the -i switch going to 'edit in place' your message template file.

Won't you need an unaltered version of the template in the backup-notification at the start of the backup process?

As an alternate method, have you considered using an echo with a string that uses the variables of interest piped into your ssmtp command?

echo this confirms that backup has been completed and renewed for $HOSTNAME on $DATE | ssmtp [email protected]
4
  • Thanks, that is a good point. I tried your suggestion and it works with the $HOSTNAME but for some reason $DATE does nothing. So the email says "This confirms that backup has been completed on server.domain.org on ."
    – Chris
    Commented Mar 10, 2016 at 23:40
  • @ecube was probably right about needing to use $(hostname) and $(date). I thought your .bashrc was set up to set the variables. You should up vote him for the fast response
    – infixed
    Commented Mar 11, 2016 at 0:14
  • You should even mark him as the correct answer, because he used sed like your question asked, as well as being fast
    – infixed
    Commented Mar 11, 2016 at 0:25
  • Thanks to both of you! Marked answer from @ecube as the correct one since it worked and was the best solution.
    – Chris
    Commented Mar 11, 2016 at 15:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .