Learning Common Lisp by using it for shell scripting

For quite a while now, Lisp, or more specifically, Common Lisp has been on my list of languages to learn.
Lack of time and suitable projects for learning has put it off for a quite a while. And, while Lisp can be a neat
language for almost everything, a significant effort is required for getting up to sufficient speed for programming a typical web application.

Getting the idea

A post I found on comp.lang.lisp
raised the subject of
using Common Lisp for shell scripting
. So, I thought that shell scripting would be perfect for learning Common Lisp. Small programs that are done quickly and are usable are a lot more motivating
than dabbling with a small part of a normal application and not getting anywhere near a finished project.

So far, it’s been pretty rewarding. I have written a small backup utility (script source) that copies a file and adds a timestamp to the filename and a script for reminding me every time one of my domain names
closes in on its expiration date (script source). Variable binding with let and multiple-value-bind, the
format directive and date handling are the most important lessons I have learned from these scripts.

Shell scripting quirks

Compared to a normal Common Lisp program, there are two things to notice. First, the shebang line
that is required for use as a shell script (#!/usr/bin/env clisp). It causes a syntax error in Lisp so I
comment it out for development. Second, the whole script is fired with a main function that takes no
arguments. The main call is also commented out for development. I have basically followed the model
from Lars Rune Nøstdal’s example script from the mentioned CLL thread
and I think the idea behind this approach is that all functions can be developed and tested
independently. When going from development to executable shell script, all that is needed is to uncoment the shebang line and the call to the main function.

You can probably use any Common Lisp implementation for this. Clisp is quite small so it works well for shell scripting and is what I have used for my scripts.

Script usage

Usage of the backup script (after making the script executable with chmod +x clbackup.lisp):

harald@semmentjern:~/prog/lisp$ mkdir test
harald@semmentjern:~/prog/lisp$ touch test/somefile.txt
harald@semmentjern:~/prog/lisp$ ls test
harald@semmentjern:~/prog/lisp$ ./clbackup.lisp test/somefile.txt 
Copying file test/somefile.txt to test/somefile.txt.20070513154857
harald@semmentjern:~/prog/lisp$ ls test
somefile.txt  somefile.txt.20070513154857

Usage of the domain alert script (with a 150 days limit):

harald@semmentjern:~/prog/lisp$ ./domainalert.lisp 150
goodwebhosting.info expires in 144 days
flaks.net expires in 150 days

3 Responses to “Learning Common Lisp by using it for shell scripting”

  1. Nick Mudge Says:

    I wish there was a way to not have to comment out the shebang with clisp. I think there might be a way with SBCL: http://www.sbcl.org/manual/Unix_002dstyle-Command-Line-Protocol.html#Unix_002dstyle-Command-Line-Protocol

  2. hb Says:

    Nice tip! I wouldn’t be surprised if there is something similar for Clisp but SBCL should work for shell scripting too. I will try to test this sometime soon.

  3. Wahoo Says:

    Thank you for sharing!