Sun Jun 14 10:36:08 CEST 2009

shell scripting and garbage collection

One of the things that has allways annoyed me is that file systems do
not have garbage collection.

The problem is really that it is not straightforward to determine
whether a process is using a file.  Ok, if the file is opened this is
simple, but often some comination of processes has a collection of
intermediate files that are not referenced but still needed in the

However, there are alternatives that are easier to use.  Having
something like "dynamic-wind" to provide a centralized cleanup point
is maybe good enough.

First, adding some dispatching to a shell script makes composition
easier.  Instead of seeing a script as a single command, simply adding
more commands makes it possible to recursively invoke locally defined
code in different contexts.  A dispatcher takes the following form:

receive () {
    if [ "$1" == send ]; then
        main "$@"

Now, the other problem is temp storage: you'd really want to abstract
this.  One solution is to always open a temp dir which will be
ruthlessly removed whenever a process finishes.

What about this:

  1. always run a "worker" script inside a "framework" script that
     provides resource management.  whenever the worker terminates,
     resources get freed.  (this is dynamic-wind).

  2. use dispatching.