Scala scripting

Scala is a very powerful programming language which helps you to build scalable applications. But in some cases it comes handy to use Scala as a scripting language. For example executing from a cronjob.

The following code snippet shows you how easy it is to invoke a scala script in a bash script. I assume you have a lib dir where your classpatch jars exist.

lib=`dirname $0` ./lib
cp=`echo $lib/*.jar|sed 's/ /:/g'`
exec scala -classpath $cp $0 $@
// Say hello to the first argument
println("Hello " + args(0))

The first part is the executing part where we pass the first argument and a pointer to the next part.
Scala knows to execute the part beneath the “!#” and uses the given arguments.

Maybe when you installed Scala and you try to run your script like this:

servername]$ ./

And you get this error

Could not find a directory for temporary files

You need to open up the permissions for the following directory as root:

chmod 777 /tmp/scala-devel

Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: