Holiday Mail Script

I wrote a script to send a greeting to all the users that want one on certain holidays.  Here it is (Korn shell):

#!/bin/ksh
#
#
# Sends mail to users on holidays defined in /etc/holidays.

# OPTIONS
# -h <file> - specifies holidays file (default /etc/holidays)
# -g <group> - group to use for sending messages (default holidays)

DATE=`date +%-m/%-d`
FILE="/etc/holidays"
GROUP="holiday"

        do_cowsay() {
        # check command line args
        until [[ $# -eq 0 ]]
        do
                set arg $*

                if [[ $arg = '-h' ]]
                then
                        shift
                        set arg $*
                        FILE=$arg
                elif [[ $arg = '-g' ]]
                then
                        shift
                        set arg $*
                        GROUP=$arg
                else
                        echo "Unown option: $arg\n" >&2
                fi

                shift
        done

        # check for cowsay
        COWSAY=`which cowsay`
        if [[ -f $COWSAY ]]
        then
                COWARGS=""
                # check for cowpath variable
                # make a list of cowfiles
                COWFILES=`cowsay -l | cut -d: -f2`
                elements=`echo $COWFILES | wc -w`
                idx=$RANDOM
                idx=$(($idx % $elements - 1))
                #XXX for testing
                #XXX echo "idx is $idx"
                #XXX end testing
                set -A COWFILES $COWFILES
                cowfile=${COWFILES[$idx]}
                COWARGS="-f $cowfile"
                FORTUNE=`fortune`
                COWSAY=`$COWSAY $COWARGS $FORTUNE`
        else
                COWSAY=$FORTUNE
        fi

        #XXX for testing
        #XXX $COWSAY \"`ddate`\"
        #XXX end testing
} # do_cowsay
AWKLINE="{if (\$1 == \"$GROUP\") print \$4}"
USERS=`awk -F: "${AWKLINE}" /etc/group | sed "s/,/\\n/g"`

#XXX for testing
#XXX echo $USERS
#XXX end testing

AWKLINE="{if (\$1 == \"$DATE\") {\$1=\"\"; print \$0} }"
holiday=`awk "!/^\*/ ${AWKLINE}" $FILE`

#XXX for testing
#XXX echo "$holiday"
#XXX end testing

for user in $USERS
do
        do_cowsay
        mail -s "Happy $holiday!" $user <<EOF
Greetings on this $holiday, $user!  Please enjoy this $holiday fortune on behalf of the self-aware machinery running lakemasoniccenter.org!

$COWSAY

-root on kitesfear.lakemasoniccenter.org
EOF
done

My holidays file:

* Prime/Nonprime Table for accounting system
*
* Curr  Prime   Non-Prime
* Year  Start   Start
*
  2011  1400    2300
*
*
* Only the first column is significant
*
* month/day     Holiday
*
  1/2           New Year's Day
  1/16          Birthday of Martin Luther King, Jr.
  2/20          Washington's Birthday
  5/28          Memorial Day                                                                 
  6/24          Feast of St. John the Baptist                                                
  7/4           Independence Day                                                             
  9/3           Labor Day                                                                    
  10/8          Columbus Day                                                                 
  11/12         Veterans Day                                                                 
  11/22         Thanksgiving Day                                                             
  12/25         Christmas Day                                                                
  12/27         Feast of St. John the Evangelist                                             

And some sample output:

Happy  Testing Day!
 From: Joshua Armstrong <imbrius@lakemasoniccenter.org>
 To: imbrius@kitesfear.lakemasoniccenter.org
 
Greetings on this  Testing Day, imbrius!  Please enjoy this  Testing Day fortune on behalf of the self-aware machinery running lakemasoniccenter.org!

 _________________________________________ 
/ Razors pain you; Rivers are damp. Acids \
| stain you, And drugs cause cramp. Guns  |
| aren't lawful; Nooses give. Gas smells  |
| awful-- You might as well live! --      |
\ Dorothy Parker, "Resume", 1926          /
 ----------------------------------------- 
      \                    / \  //\
       \    |\___/|      /   \//  \\
            /0  0  \__  /    //  | \ \    
           /     /  \/_/    //   |  \  \  
           @_^_@'/   \/_   //    |   \   \ 
           //_^_/     \/_ //     |    \    \
        ( //) |        \///      |     \     \
      ( / /) _|_ /   )  //       |      \     _\
    ( // /) '/,_ _ _/  ( ; -.    |    _ _\.-~        .-~~~^-.
  (( / / )) ,-{        _      `-.|.-~-.           .~         `.
 (( // / ))  '/\      /                 ~-. _ .-~      .-~^-.  \
 (( /// ))      `.   {            }                   /      \  \
  (( / ))     .----~-.\        \-'                 .~         \  `. \^-.
             ///.----..>        \             _ -~             `.  ^-`  ^-_
               ///-._ _ _ _ _ _ _}^ - - - - ~                     ~-- ,.-~
                                                                  /.-~

-root on kitesfear.lakemasoniccenter.org
Advertisements

, , , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: