Jump to content

Domain-Specific Languages--in bash?

0
  mike-loukides's Photo
Posted Nov 10 2009 12:38 PM

That's perhaps overstating it, but you can use bash shell functions to create things that are very similar to domain-specific languages. Here's a stripped-down extract from one of my scripts, which does an SVN up on all the version control archives I follow:
UpdateSvn()
{
  MYSELF=/Users/mikel
  if [ "$2" == "" ] 
  then
     dir=svn
  else
     dir=$2
  fi

  cd ${MYSELF}/$1
  pwd
  svn update $dir
  echo
}

UpdateSvn thing1
UpdateSvn thing2
...


It would be possible, and possibly desirable, to move all the UpdateSvn commands nto a separate file, which the script would read and exec.

UpdateSvn isn't much of an DSL, but I've got other functions for different kinds of archives--UpdateFTP, UpdateWeb (using wget), UpdateDARCS, UpdateMercurial, etc. The shell programming here is REALLY trivial, but it also meets the simplest requirements for a DSL: you could give someone with no knowledge of shell programming a list of directories, and the type of archive in each directory, and he or she could write the "update" script.

It would also be possible for a masochist to do some true metaprogramming (build up functions at runtime as strings, exec them to define the functions, etc.). That's left as an exercise for the perverse. Actually, it sounds pretty cool to me, though I'm not sure why I'd do it.

BTW, this is not in the Bash Cookbook, though arguably it should be.

Tags:
0 Subscribe


0 Replies