Tuesday 7 July 2020

Linux mail - a Linux shell script to send mail

Linux Mail, LPI Study Material, LPI Exam Prep, LPI Tutorial and Material, LPI Study Material

Here's a n example Linux shell script (Bourne shell to be specific) that I use to send a list of directories to one of our invoicers. She uses this list as part of a cross-checking process to make sure she bills each one of our customers who have a directory allocated to them. The list is sent to her automatically from a Linux crontab entry I created for her.

Also Read: 101-500: Linux Administrator - 101 (LPIC-1 101)

Without any further ado, here is the Linux shell script that sends the email. (Note that although I keep writing "Linux mail", this should also work on other Unix systems.)

Example Linux shell script - sending mail


Here's the source code for the Linux shell script that sends the email message out every day:

#!/bin/sh

cd /home/lpicentral/bin

echo "

DO NOT REPLY TO THIS EMAIL MESSAGE.

This is a list of the current customer directories installed on our server.
Please make sure they are all billed.

"                       >  listOfWebSites
ls -1 /customers        >> listOfWebSites

mail -s "Current customer directories on our server" kim@herdomain.com < listOfWebSites

That script creates a file named listOfWebSites, then sends that file via the standard Linux mail command.

Corresponding Linux crontab entry


Here is the crontab entry that I use to send this email message every day. If you're familiar with the crontab format you should be able to easily figure out the date and time this information is mailed to here.

30 7 12 * * /home/lpicentral/bin/mailWebSitesToKim

I thought this was a pretty decent use of shell scripting and the built-in Linux mail command. Notice how easy the Unix operating system and redirection operators make this.

Related Posts

0 comments:

Post a Comment