Finally, I'm going to finish the mail merge script, just in time for Replicant Day.
Remember the mail merge script I started writing a while back? Yeah, that was quite some time ago. I got sidetracked with the Linux Journal Anniversary special issue (see my article "Back in the Day: UNIX, Minix and Linux"), and then I spun off on a completely different tangent for my last article ("Breaking Up Apache Log Files for Analysis"). I blame it on...
Oh, sorry, back to topic here. I was developing a shell script that would let you specify a text document with embedded field names that could be substituted iteratively across a file containing lots of field values.
Each field was denoted by
#fieldname#, and I identified two
categories of fieldnames: fixed and dynamic. A fixed value
#name#, which would come directly out of the data
file, while a dynamic value could be
#date#, which would be
the current date.
More interesting, I also proposed calculated values,
#suggested#, which would be a value calculated
#date#, which would be replaced by
the current date. The super-fancy version would have a
simple language where you could define the relationship between
variables, but let's get real. Mail merge. It's just mail
Reading and Assigning Values
It turns out that the additions needed for this script aren't too difficult. The basic data file has comma-separated field names, then subsequent lines have the values associated with those fields.
Here's that core code:
if [ $lines -eq 1 ] ; then # field names # grab variable names declare -a varname=($f1 $f2 $f3 $f4 $f5 $f6 $f7) else # process fields # grab values for this line (can contain spaces) declare -a value=("$f1" "$f2" "$f3" "$f4" "$f5" "$f6" "$f7")
declare function turns out to be ideal for this,
allowing you to create an array
varname based on the
contents of the first line, then keep replacing the values
of the array
value, so that
value, and so
To add the additional variables
simply can append them to the
value arrays. The
first one is easy, but it did highlight a weakness in the
original code that I had to fix by adding quotes as shown: