Karl Hans Janke Kollaborativ
Heute die Welt, morgen das Sonnensystem!
<< prev next >>

Keeping a personal journal on plain old Unix

The other day, somebody presented this privacy nightmare on Hacker News. Nevertheless, I thought the idea was cute. Encourage people to write a journal entry every day, combined with a random peek at one of their old entries. Then make it really easy for them to drop the new one.

Here is a pair of shell scripts towards the same end. Entries are kept in ~/journal, one plain text file for each year. Each entry is automatically marked with the date and time of its submission. The format is trivial, give it a try or read the code.

To add a new entry, call the journal script. I alias this to the shortcut j.

# keeping a personal journal, pesco 2010
[ -z "$EDITOR" ] && EDITOR=vi
[ -d "$HOME/journal" ] || mkdir "$HOME/journal"

t=`mktemp -t journal`
if cmp -s $t /dev/null; then
	echo "empty file, nothing to do"
	f="$HOME/journal/`date +%Y`"
	( date
	  cat $t | sed -e 's/^/> /'
	) >> $f
	echo "got it. thanks for saving this!"

It drops you into an editor on an empty file. Just type anything on your mind, save, quit, done. And you even get a nice thank you for it.

To see a random entry, call the past script. This one is a little longer because of the whole business of getting random numbers, selecting a file, and picking one specific entry out of it. Yet it uses only standard Unix tools. Have a look:

# retrieving a random journal entry from the past, pesco 2010

rand() {
	od -N 2 -t u2 /dev/urandom | sed -e 's/^[^ ]*//'

# select a journal file
n=`ls $HOME/journal | wc -l`               # number of files
i=`expr $r % $n + 1`                       # random index, 1-based
f=`ls $HOME/journal | sed -n -e "${i}p"`   # that file basename
f="$HOME/journal/$f"                       # absolute path

# select a journal entry
n=`grep '^ *$' $f | wc -l`                 # number of entries
i=`expr $r % $n`                           # random index, 0-based

# print journal entry
	# skip until the i'th empty line
	while [ $j -lt $i ]; do
		read l
		if [ -z "$l" ]; then
			j=`expr $j + 1`

	# echo until the next empty line
	read l
	while [ -n "$l" ]; do
		echo "$l"
		read l
) < $f

According to the HISTORY sections of my corresponding man pages, this should run on Version 1 AT&T Unix and later! Note the use of expr for arithmetic operations. You can run this on your mainframe! :)

I swear, somewhere in the multiverse there is an alternate timeline forking off in the 70s mainframe era and it's totally awesome.

Have fun!

PS. Right, this doesn't remind you to submit your daily report. I actually don't like the part about friendly reminder emails piling up in my inbox that much.