

First steps with Scala, say goodbye to bash scripts… - opensas
http://playlatam.wordpress.com/2011/12/05/first-steps-with-scala-say-goodbye-to-bash-scripts/
Playframework 2.0 is right around the corner, and it’s core is programmed in Scala, so it’s a wonderful opportunity to give this object-oriented / functional hybrid beast a try…
Like many others, I will pick a very simple script to give my first steps…
======
moonboots
I still prefer a simple shell script for these use cases. Here's a snippet for
the translated file size calculation:

    
    
      translated_files=$(grep "todavida no ha sido traducida" *)
      total_size=$(du --summarize . | cut -f 1)
      translated_file_size=$(du --summarize --total $(echo translated_files) | tail -n 1 | cut -f 1)
      echo "translated size: ${translated_file_size}KB/${total_size}KB $(($translated_file_size/$total_size))%"

~~~
archangel_one
I find that much more elegant; four lines of bash script which would run on
nearly any Unix-like system, versus 49 (or a bit less if you drop the
whitespace) of Scala, which won't. Even the shebang is three lines instead of
one!

I guess this example is nice as an introduction to Scala, but it doesn't seem
like Scala is a particularly appropriate replacement for any of the things one
would normally use a shell script for.

~~~
viraptor
> which would run on nearly any Unix-like system

Did you mean on nearly any Unix-like system which has the output of du in the
same format as the one on your machine? I don't believe those can be treated
as standard if you want any kind of portability.

~~~
archangel_one
Okay, I'm not an expert on variations between different Unices, that may well
be the case. The point I was trying to make was more that it would run without
having to install a great wodge of Java/Scala dependencies which aren't on
most systems by default.

------
jcdavis
A nice article, but its worth mentioning that scala for bash script usage is a
non-starter for most stuff you might use bash for because JVM startup
overhead, even for the client VM, is so substantial

~~~
gaius
See [http://www.scribd.com/doc/36045849/Practical-Haskell-
Program...](http://www.scribd.com/doc/36045849/Practical-Haskell-Programming-
scripting-with-types)

Type safety, much better type inference than Scala, and no JVM overhead.

------
ww520
I don't get how the bash scripts are not used. It's still used at

    
    
        #!/bin/sh
        exec scala "$0" "$@"

~~~
alexatkeplar
That's a one-line Bash wrapper to invoke Scala (line 2) with the original
arguments sent to the Scala script, compiling the Scala code which follows
below. You can read more at the bottom of the man page here:
<http://www.scala-lang.org/docu/files/tools/scala.html>

~~~
slashclee
I wonder why they don't use

#!/usr/bin/env scala

as the first line instead. If there's some hack to support #!/bin/sh in the
scala interpreter... ew.

~~~
opensas
I tried it, and got:

sas@ubuntu:~/devel/apps/playdoces/documentation/1.2.4/manual/tmp$
./status.scala error: script file does not close its header with !# or ::!#
one error found

~~~
slashclee
If you do:

    
    
        #!/usr/bin/env scala
        !#
    

it appears to work.

------
opensas
Thanks to everybody's feedback on this one... You encouraged me to write
another article, mainly because I felt a little guilty for bashing (pun
intented) bash and python ;-) Hope you like it
[http://playlatam.wordpress.com/2012/01/13/first-steps-
with-s...](http://playlatam.wordpress.com/2012/01/13/first-steps-with-scala-
revisited-bash-and-python-strikes-back/)

------
jezclaremurugan
nice article, but they could have removed the last part of the title. Its a
nice way to learn scala, but a poor way to say goodbye to bash.

~~~
opensas
you're right jez, I guess I couldn't resist the temptation of a catchy title,
just changed it to: First steps with Scala, a functional alternative to bash
scripts… I think it's more appropriate:
[http://playlatam.wordpress.com/2011/12/05/first-steps-
with-s...](http://playlatam.wordpress.com/2011/12/05/first-steps-with-scala-
say-goodbye-to-bash-scripts/)

~~~
jezclaremurugan
sounds great now, i loved it anyway!

------
ronreiter
What's wrong with Python?

~~~
opensas
nothing - [http://playlatam.wordpress.com/2012/01/13/first-steps-
with-s...](http://playlatam.wordpress.com/2012/01/13/first-steps-with-scala-
revisited-bash-and-python-strikes-back/#conclusion)

