[TAG] question and announcement, sort of...

Ben Okopnik ben at callahans.org
Wed Jul 14 08:55:41 MSD 2004


On Wed, Jul 14, 2004 at 04:19:10AM +0200, Robos wrote:
> On Tue, 13.07.04, Ben Okopnik <ben at callahans.org> wrote:
> 
> > Well, seems like setting up Mailman or another list manager would be the
> > answer - lots of flex for later expansion. However, here's a pretty
> > simple script that should do what you're asking:
> > 
> > ----------------------------------------------------------------------
> > #!/bin/bash
> > # Created by Ben Okopnik on Tue Jul 13 11:45:09 EDT 2004
> > 
> > [ -z "$2" ] && { printf "Usage: ${0##*/} <list> <file> [file] ... \n"; exit; }
> > 
> > files=`echo "$@"|sed 's/^[^ ]* //'`
> > 
> > for n in `cat $1`; do cat $files|mail -s 'Command-a-day' $n; done
> > ----------------------------------------------------------------------
> 
> Hmm, I see I didn't make myself clear enough: there are 2 groups
> _at_the_moment_ but I would like the skript to be so flexible that it can
> handle incoming new persons or groups and see to it that all get the
> follow-up command to their previous mail.
> I would do something like:
> -a list with all the recipients
> -a list where the current "command position" for each recipient is noted and
> incremented
> -a list with all the commands.

So, more like this, then:

----- list.txt --------------------------------------------------------
foo at bar 7
bar at foo 3
joe at xyz 22
ann at abc 0
-----------------------------------------------------------------------

----- do_it_to_it -----------------------------------------------------
#!/bin/bash
# Created by Ben Okopnik on Wed Jul 14 00:31:28 EDT 2004

work_dir="/some/where/over/the/rainbow"
max=`ls $work_dir|sort -n|tail -1`

while read mail last
do
        [ "$last" = "" ] && continue
        [ "$((last + 1))" -gt "$max" ] || { 
	((last++))
        /usr/bin/mail -s "Command-A-Day" $mail < $work_dir/$last
        [ "$?" -gt 0 ] && echo "Problem with $mail!" ||\ 
		echo "$last sent to $mail"
	}
        list=`echo -e "$list\n$mail $last"`
done < $work_dir/list.txt

OLD=$IFS; IFS=; echo $list|sed '/^$/d' > $work_dir/list.txt; IFS=$OLD
------------------------------------------------------------------------

Here's how it works: you define a working directory in "work_dir", and
place your numbered "command" files in it. You also create a "list.txt" in
there, with the syntax shown above; every new entry starts at 0. There
should not be any other files in that directory, just to be safe. Every
time the script is run, it will

2) Email the appropriate "next" file to everyone who has not reached $max
3) Increment the numbers where "mail" executed OK
4) Write the results back out to "list.txt".

Note that "$max" will change whenever you add a new, higher-numbered file
to the directory, and the people who were "maxed out" before will get
whatever you added.

It's a little fragile - if the script somehow manages to die in the middle
of the process, the numbers in the file will not be incremented - but
that's not a horrible thing.


* Ben Okopnik * okopnik.freeshell.org * Editor-in-Chief, Linux Gazette *
-*- See the Linux Gazette in its new home: <http://linuxgazette.net> -*-




More information about the TAG mailing list