Slothoughts Hypolite

Yet another place for musings from a guy in the choir 
« Back to blog

Finally ... A Real Psalm 'o Matic

Several years ago, it occurred to me to create a Psalm-o-matic for Lent. I have no idea why this occurred to me, it just popped into my head one day and I couldn't forget it. In theory, we are supposed to be a bit more focused on spiritual things, like scripture and prayer, during Lent. Just because we're Congregationalists—UCCs actually—why shouldn't we attempt to bow to tradition, at least a bit. So to make the scripture part a little more palatable—or so I thought at the time—how 'bout if we each read a Psalm each day? Rather than expect people actually to hunt up a Bible, and then figure out where the Psalms lay in that book, why not just e-mail out a Psalm? If we're to read Psalms, why focus on the three or four we already know. How 'bout we read a randomly selected Psalm? That way, in theory at least, we would get through Lent with a representative selection of Psalms, the familiar and the not-to familiar.

 Thus was born the idea of the Psalm o' matic. Now to figure out how to implement it. Of course, I thought it would be cool to write a computer program to do the work for me. I had a job at the time, and was a bit rusty on my programming, so I ended up with the coward's way. I used the random-number generator function in my spread sheet program to make up a list of numbers between 1 and 150. Then I copied the list over to a schedule, printed it out, and manually e-mailed out the Psalm scheduled for each day. Not all that o' matic, huh?

 I did that for a couple of years, but this year, since I was unemployed, I figured that I should brush off my very rusty shell scripting and try to make a real, bone fide psalm-o-matic. The shell script would, in theory at least, randomly pick out a number from 1 to 150, select the Psalm that related to that number, and do the e-mailing for me. All while I slept.

 The first thing I needed to do was come up with a method for generating random numbers from 1 to 150. Of course that's trivial if you are programming in C or C++, or using a spread sheet, but I was trying to do this in Unix shell scripting, the only way I knew how to send out e-mails automagically. It turned out that what I knew about sending out e-mails—learned in Intro to Unix back in the summer of 2000—was no longer valid, at least not on my system. But I didn't know that when I began this project.

 Anyway, it turned out that generating random numbers from the unix shell was relatively easy. My server at Joyent runs bash-shell scripts, and the bash shell has a build-in shell variable called $RANDOM. $RANDOM returns a random number from 0 to 32767 whenever it is called. To pare this selection down to 1 to 150, I merely had to multiply the $RANDOM variable by 150, divide by 32767, and then add 1 to take care of the fact that Psalms begin with 1 not 0 (zero).
psalmNo=$((1+150*$RANDOM/32767))
and the variable, $psalmNo would indeed be a number between 1 and 150.

 So far, so good. Now I just needed to select the appropriate Psalm and send it off. Selecting the appropriate Psalm was trivial, albeit tedious. I made up 150 text files with names like psalm_xxx.txt, where xxx was a number from 1 to 150. Then I just needed to combine three elements, the root of the text-file name, the Psalm number and the extension:
body="$filePath$fileRoot$psalmNo$fileExtension"
Oh, yeah, I forgot to mention, the $filePath thing tells the computer where to find the directory that has all the Psalms in it.

 So now we get to what I assumed would be the easy part, e-mailing the Psalm. In olden days, we did something like the following:
mailx -s "e-mail subject" someone@somedomain.com <$body
That is, the file in $body is inserted in an e-mail message to someone with the subject given. Well, it didn't work. After thrashing around for quite some time, I asked for help and was told that mailx no longer worked because of security issues. I had to use sendmail -t instead.

 I'd never heard about sendmail -t before. I guess it didn't work in the olden days. Or perhaps it just didn't work at UMass/Lowell, who tend to be a bit behind the times (when I took Java Script, I was told to be wary about things that caused problems with Netscape 2.0. WTF? we were in a post-Netscape era by the time I took that class.)

 Anyway, I had to learn about sendmail. That wasn't overly difficult. I discovered that the -t option meant that the system used the header information embedded in the file. So eventually, I figured out that meant I had to make up the files I e-mailed out with stuff like "To: someone@somedomain.com" at the top of the file, and the text file containing the Psalm at the end. Well, crap, I didn't want to go through 150 files and put in all the header stuff first. What if I wanted to do something else with the files later on?

 Fortunately, this wasn't a problem. I suddenly remembered about "appending" things to a file. To append things, you just did a double carat thingie, i.e.
cat $body<<$filePath$mailMsg
and you get the stuff in $body, i.e. the Psalm file, appended to $mailMsg, the thing you created with all the header info. So I build it up a bit at a time, starting with the "To: someone@somedomain.com" stuff, adding in the From, Reply-to, and Subject stuff and finally ending with the Psalm file.

 So after all this ruminating, we ended up with

# !/usr/local/bin/bash 
# psalm o' matic script
#
# This works as follows: me@myServer$ /usr/local/bin/bash psalm-0-matic
# If you don't precede with the "bash" it won't work.
# The fully qualified paths through out are necessary to make this work as a cron script
#
# This uses my random number generator script
#
# Why in the hell am I using vi?
# I'm not anymore -- using TextPad with sftpDrive (now ExpanDrive)

 filePath="/users/home/myUserName/domains/myDomain.org/web/public/Psalms/"
fileRoot="Psalm_"
fileExtension=".txt"

 # ***seed Random with a "random" number -- then select Psalm o' day

 RANDOM=$RANDOM

 psalmNo=$((1+150*$RANDOM/32767)) #all 150 -- Oh, yeah!

 # *****************prepare the message ******************

 mailMsg="MailMsg"

 today=`/usr/xpg4/bin/date '+%A, %B %d, %Y'`

 # ***************** prepare header header ******************

 dateField="Date: "$today
#toField="To: me "
#later on it would be fccr-ace.
addressFile=pomList
toField="To: "`cat "$filePath$addressFile"` #pomList is comma delimited: name , ...
fromField="From: me "
replyToField="Reply-to: me@myOtherDomain.net" #later on it would be fccr-ace?
subjectField="Subject: Psalm for "$today"--Psalm "$psalmNo

 # ***************** assemble message ******************
body="$filePath$fileRoot$psalmNo$fileExtension"

 echo $dateField<$filePath$mailMsg
echo $toField<<$filePath$mailMsg
echo $fromField<<$filePath$mailMsg
echo $replyToField<<$filePath$mailMsg

 echo $subjectField<<$filePath$mailMsg
echo " " <<$filePath$mailMsg
echo " " <<$filePath$mailMsg
cat $body<<$filePath$mailMsg

 # *****************send the message ******************

 /usr/local/bin/sendmail -t <$filePath$mailMsg

 Ok, this worked. Getting it to send out automagically turned out to be more trouble than I realized, but I eventually got so I was sending myself Psalms on an hourly basis. Just in time for Lent.

 Addendum:
The scheme worked like a charm and we lucked out in that Ps. 119 never came up. Not sure anyone would have been able to slog through that one in one sitting.

 Addendum 2:
Formatting this was a real PITA. I'd have been better off just doing raw xhtml on my static site. Then again, I suppose that I needed to (re?)learn that'code' is not a block-level element.

Comments (5)

Apr 22, 2009
Roy said...
So - with this approach - was there ever an issue of getting the psame psalm psent out in the course of the 150?
Apr 22, 2009
Larry Piper said...
You can't have a truly random selection, without eventually getting some duplicates. I suppose I could have added to the script to weed those out, but that seemed like cheating. Over the course of the 40 plus days of Lent (I didn't shut down on Sundays, as I should have were I being technically correct from a liturgical point of view), we ended up with three duplicates, Psalms 38, 101 and 102. Not too bad, lots of variety, which means a goodly dose of the gloomy ones that piss people off. Weird how people rhapsodize how much they love the Psalms, but when they are forced to read more than a select few, they get pissed off.
Apr 22, 2009
Roy said...
Personally - I love the psalms of lamentation. 88, 69, 22 - all faves for me. Maybe it's because my experience of G-d is one more of absence than consolation.. who knows.

14 I am poured out like water,
and all my bones are out of joint.
My heart has turned to wax;
it has melted away within me.

15 My strength is dried up like a potsherd,
and my tongue sticks to the roof of my mouth;
you lay me [b] in the dust of death.

16 Dogs have surrounded me;
a band of evil men has encircled me,
they have pierced [c] my hands and my feet.

17 I can count all my bones;
people stare and gloat over me.

18 They divide my garments among them
and cast lots for my clothing.

Yeah man.. right to the heart of the matter.

All this psalm-talk has me thinking.. I have a book-present for you. I picked it up while on retreat at the Abbey of the Genesee about 8 years ago... I'll bring it to the Temple on Sunday.

Apr 22, 2009
Larry Piper said...
Only one of those three showed up during Lent, Ps. 88 on April 3. The other two showed up in my pre-Lenten tests, Ps. 22 on Feb 19 and Ps. 69 on Feb 21.
 
We sang an anthem based on Ps. 22 one week. I think a lot of folks were freaked out. But I'm with you. The first time I read one of these dismal ones I thought, "whoa, here's a dude who knows depression first hand".
 
I like this verse from Ps. 69:
"13 They who sit at the gate gossip about me; drunkards make me the butt of their songs."
I dunno, it you think about it, you are providing those drunkards a real service by giving them song topics.
 
On another topic, it's amazing how excited everyone is about going to the temple. We have 30 folks on the list now. Should be interesting. Maybe the kids will start to incorporate the Noble Eightfold Path into their statements of faith. It could happen, we're not exactly orthodox.
Sep 17, 2009
larry p said...
I can't believe you got all the damn carrots pointing in the wrong direction! WTF?

Yo, anyone who reads this. The author clearly meant '>' when he wrote '<'. Some folks are just directionally challenged. Cut him some slack.

Leave a comment...

 
Got an account with one of these? Login here, or just enter your comment below.
Posterous-login    Connect    twitter