My first shell script: bundlemaker

May 14, 2008 – 9:31 pm

Technical Thing For The Day: Achieved!

Today I learned how to write shell scripts - or that is, I wrote my first shell script that didn’t involve a straight cut-and-paste from my .history file. (for my parents: a “.history file” is something that contains the last bunch of commands you’ve typed into your computer, and is useful for going back and figuring out what you’ve just done… sort of like an instant replay.)

I spent way too long tracking down Boston-area housing for the ILXO
people who will be in the Chicago office with me this summer (they’re
going to be at 1cc for a while for training), so going to count this script because it’s the only tech thing I’ve got to show for today. (So far.) The script itself was written in little bits and pieces during a meeting when I couldn’t hear the people who were talking; it’s a quick hack.

It’s a content bundle making script that I whipped up for Chris, who was working on creating health content bundles from MedLinePlus. Basically, if you point the script at an “index.html-like” page on the web that has links to articles and files that you’d like to include in a bundle, the script will download that page and all the files it points to, generate library metadata for the XO to interpret, then pack it all up in an .xol library bundle file.

Writing shell scripts is surprisingly easy. I was psyching myself up for it to be hard and all sorts of elaborate arcana. I should not do that. It is not hard. Really, you just type in the commands you’d type into the shell, use $variables in appropriate places, and look up the proper syntax for simple logic loops (one nitpick: I wish they’d use mathematical notation for boolean logic instead of making me remember that “-ne” means “not equals” instead of the “!=” I’m used to. This is entirely my fault, as I am a creature of habit).

The script could use improvement - more to the point, my script-writing-fu could use improvement as there are kludges in there I’m not satisfied with (for instance, the section that creates a library.info is incredibly brittle; it just concatenates the appropriate lines into a text file), so comments and changes and pointers to cool examples of good script-writing practice are super-welcome.

Perhaps I should go back to visit NYC and get my copy of Beautiful Code back from Ethan someday. That has some gorgeous code to read.

  1. 2 Responses to “My first shell script: bundlemaker”

  2. hey Mel, I DRY’ed your script up a bit and updated the wiki. Note the (echo echo echo) pattern for the ini file, it’s pretty handy. The paren’s basically open a second shell so a sequence of commands can be redirected, etc. as one.

    By dwins on May 17, 2008

  3. Here are some suggestions:

    - Set the arguments $1, $2, $3 tolocal variables to make it more readable, e.g. URL=$1, DEST=$2, LANG=$3

    - Do some basic validation of the input arguments - e.g. URL starts with http, ftp, etc., DEST does not already exist as a directory, filename or $2.xol, DEST does not contain a slash “/”, LANG is a supported language

    - Check return codes of key commands you execute - i.e. “if [$? -ne 0] then” — in many instances, this is a fatal error so you’d cleanup and exit with some message. For example, check the return codes of wget, mkdir and zip.

    - Use ‘rm -rf “$2″‘ with extreme caution - what if someone specified “/” as $2? As noted above, you should ensure that $2 does not already exist, etc.

    By Tom on May 18, 2008

What do you think?