News for hackers. Stuff that matters.

First steps with Scala, say goodbye to bash scripts…

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…
( 25 comments | 23 points | on HN )
Re:First steps with Scala, say goodbye to bash scripts… by moonboots on Thursday January 12, 2012 @03:02AM (#3455476)
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))%"
Re:First steps with Scala, say goodbye to bash scripts… by icebraining on Thursday January 12, 2012 @06:49AM (#3455983)
Two nitpicks: grep by default returns lines that match, not files, you need the '-l' option; and you're passing the string "translated_files" to du instead of the actual files, you need just $translated_files instead of the echo.

And that breaks with files with spaces on them, which is a bane of shell scripts.

By the way, in Python:

    import os
    total_size = sum([os.path.getsize(filename) for filename in os.listdir('.')])
    matches = [match for match in os.listdir('.') if "todavida no ha sido traducida" in open(match).read()]
    translated_file_size = sum([os.path.getsize(filename) for filename in matches])
    print "translated size: %dKB/%dKB %d" % (translated_file_size, total_size, translated_file_size / total_size)
Re:First steps with Scala, say goodbye to bash scripts… by moonboots on Thursday January 12, 2012 @12:19PM (#3457089)
Good catches

The spaces in filenames can be handled by having grep and du communicate with null delimited strings instead of newline delimited, e.g.

  grep --files-with-matches --null "todavida..." *
  du --summarize --total --files-from=<($(echo $translated_file_size))
Re:First steps with Scala, say goodbye to bash scripts… by archangel_one on Thursday January 12, 2012 @03:50AM (#3455587)
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.

Re:First steps with Scala, say goodbye to bash scripts… by viraptor on Thursday January 12, 2012 @07:09AM (#3456030)
> 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.

Re:First steps with Scala, say goodbye to bash scripts… by archangel_one on Thursday January 12, 2012 @05:10PM (#3458197)
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.
Re:First steps with Scala, say goodbye to bash scripts… by opensas on Friday January 13, 2012 @02:36AM (#3459593)
Why wouldn't it run on any unix, linux, or windows, or wherever a JVM can run? you should run it with "scala status.scala"

apart from that, I came to a similar conclusion: http://playlatam.wordpress.com/2012/01/13/first-steps-with-s...

Re:First steps with Scala, say goodbye to bash scripts… by opensas on Friday January 13, 2012 @02:32AM (#3459585)
thanks for the tip, here's my bash version http://playlatam.wordpress.com/2012/01/13/first-steps-with-s...
Re:First steps with Scala, say goodbye to bash scripts… by opensas on Friday January 13, 2012 @02:33AM (#3459589)
Re:First steps with Scala, say goodbye to bash scripts… by jcdavis on Thursday January 12, 2012 @01:39AM (#3455295)
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
Re:First steps with Scala, say goodbye to bash scripts… by gaius on Thursday January 12, 2012 @04:20AM (#3455635)
See http://www.scribd.com/doc/36045849/Practical-Haskell-Program...

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

Re:First steps with Scala, say goodbye to bash scripts… by alexatkeplar on Thursday January 12, 2012 @03:49AM (#3455584)
True, but: 1. There are a ton of long-running/cron-style tasks where JVM startup overhead just isn't relevant (and Scala's type safety, access to Java libraries and logging tools rinse Bash), and: 2. If you are using Scala scripting in "live operator" mode, here's a good SO answer on how to speed that up: http://stackoverflow.com/questions/4237863/improving-scala-s...
Re:First steps with Scala, say goodbye to bash scripts… by opensas on Friday January 13, 2012 @02:38AM (#3459596)
I've found out that with the -savecompiled option the delay is not so pitiful... see http://playlatam.wordpress.com/2012/01/13/first-steps-with-s...
Re:First steps with Scala, say goodbye to bash scripts… by ww520 on Thursday January 12, 2012 @01:41AM (#3455303)
I don't get how the bash scripts are not used. It's still used at

    #!/bin/sh
    exec scala "$0" "$@"
Re:First steps with Scala, say goodbye to bash scripts… by switch007 on Thursday January 12, 2012 @02:30AM (#3455403)
/bin/sh is probably a symlink to something other than bash though. It's not using any features/functions of the shell other than to execute scala. It's a stretch calling that a script.
Re:First steps with Scala, say goodbye to bash scripts… by alexatkeplar on Thursday January 12, 2012 @03:56AM (#3455598)
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
Re:First steps with Scala, say goodbye to bash scripts… by slashclee on Thursday January 12, 2012 @04:22AM (#3455644)
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.

Re:First steps with Scala, say goodbye to bash scripts… by opensas on Friday January 13, 2012 @02:27AM (#3459567)
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

Re:First steps with Scala, say goodbye to bash scripts… by slashclee on Tuesday January 17, 2012 @05:16AM (#3474406)
If you do:

    #!/usr/bin/env scala
    !#
it appears to work.
Re:First steps with Scala, say goodbye to bash scripts… by opensas on Friday January 13, 2012 @02:31AM (#3459579)
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...
Re:First steps with Scala, say goodbye to bash scripts… by jezclaremurugan on Thursday January 12, 2012 @06:42AM (#3455961)
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.
Re:First steps with Scala, say goodbye to bash scripts… by opensas on Thursday January 12, 2012 @11:09PM (#3459155)
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...
Re:First steps with Scala, say goodbye to bash scripts… by jezclaremurugan on Friday January 13, 2012 @10:40AM (#3460975)
sounds great now, i loved it anyway!
Re:First steps with Scala, say goodbye to bash scripts… by ronreiter on Thursday January 12, 2012 @04:53AM (#3455705)
What's wrong with Python?
Re:First steps with Scala, say goodbye to bash scripts… by opensas on Friday January 13, 2012 @02:43AM (#3459617)