A sample Unix/Linux shell script that mails contents to a user

By Alvin J. Alexander, devdaily.com

Here's a 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 double-check 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 crontab entry I created for her.

Without any further ado, here is the Bourne shell script that does the trick. (Note that it should work on both Unix and Linux systems.)

#!/bin/sh

cd /home/alexander/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

Here is the crontab entry that I use. 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/alexander/bin/mailWebSitesToKim

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

I hope this script can help you, or even prompt you to do something similar to this for your own needs.


devdaily logo