Scala shellscripts

No, writing executable scala shell scripts is not rocket science. I know. I noticed however that people don’t often use scala in this fashion because it didn’t occur to them.

Have a short look at the following utility I wrote the calculate the scrabble® score for every line in the given input file. The ‘magic’ is in the first lines (which I learned
here) of the file, which passes the filename and arguments to a scala command (which must be in you path to work for this).

I you put this code in a file and make it executable (ie. chmod a+x <nameOfScriptFile>) you can execute it like

./<nameOfScriptFile> fileToScore

Cool!

This entry was posted in scala. Bookmark the permalink.

5 Responses to Scala shellscripts

  1. Volgens http://www.artima.com/forums/flat.jsp?forum=282&thread=220878 moet het ook werken met deze twee regels als shebang:

    —-
    #!/usr/bin/env scala
    !#
    —-

    !# op de tweede regel is wel wat vreemd (voor Groovy is #!/usr/bin/env groovy genoeg, zelfde voor Python, etc.), maar Scala’s ScriptRunner schijnt dat om de een of andere reden nodig te hebben.

  2. peter says:

    Yep,

    #!/usr/bin/env scala
    !#
    

    is also valid, and probably a bit clearer. Thanks!

  3. Just for fun I ported your code to Groovy: http://gist.github.com/618271

    I’m sorry, but I think Groovy definitely wins here. :o )

  4. peter says:

    Just for fun I refactored to win from the groovy version:

    http://gist.github.com/618308

  5. peter says:

    So… I timed a scala, groovy and java version on a huge file to get the following metrics:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>