For the longest time I have used Ruby as my pocket knife for system scripting… you know, just knocking up small little executable files that run helpful tasks or automate yawnful processes. I like Ruby for this kind of thing and it works. Recently a coworker in marketing wanted some automation for something relatively simple and instead of using Ruby I thought i’d just knock it together with Scala and in doing so I came across a neat little thing with the Scala scripting support.
Assuming you have Scala installed, you can execute bash statements and pass them directly to your Scala code. This can be pretty handy as there are certain things that are particularly annoying to do from Scala (and more broadly with Java) like finding out where exactly you are executing too on the file system. Often if you use the good ol’ protection domain trick it will give you a location in /var/tmp, as opposed to the real location of the script. Consider the following:
#!/bin/sh
SCRIPT="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")"
DIR=`dirname "${SCRIPT}"}`
exec scala $0 $DIR $SCRIPT
::!#
import java.io.File
object App {
def main(args: Array[String]): Unit = {
val Array(directory,script) = args.map(new File(_).getAbsolutePath)
println("Executing '%s' in directory '%s'".format(script, directory))
}
}
Notice the base code between #!/bin/sh and ::!#. This allows you to execute bash script (or whatever script you want) before evaluating this file as a Scala script. This can be pretty handy for certain tasks when doing system scripting
Assume you saved this file as “thing”, you can then execute it like any other script: ./thing
Enjoy.


val List(directory,script) = args.map(new File(_).getAbsolutePath)
You even get a free RuntimeException if the user passes an invalid number of arguments.
Clearly this is a pure example of mixing the two, but yeah, your suggestion is of course more robust
Scala is a perfect choice for system scripting, unlike java.
I’m using less verbose version of scripts (without object app etc.) for scripting. Also, my scripts don’t contain that 4-line things at their tops, like in your example; just #!/usr/bin/env scala.
Script example: https://github.com/paulmillr/paulmillr.github.com/blob/master/_bin/newpost
Paul – of course I know you can write general scripts. I wanted to show how you can intermingle bash and Scala within the same script, thus having an encapsulated file and some potentially useful workarounds for things that are typically annoying to do on the JVM.
Its great that scala is becoming available for scripting.
I would suggest also to import some of the
scala.tools.nsc.io classes e.g. File, Directory
until java7 better path classes make file operations more concise or some abstraction is introduced in the real scala library.
You also probably know it, but just for the record so others can also have this information.
If you add -save when calling the scala executable, you get a compiled jar containing your script. Next time you run it, you’ll not have to compile it once again.
Very useful when using Scala as a scripting language.