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:
[ Parent
| on HN ]
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)
yeap, python is pretty cool, see http://playlatam.wordpress.com/2012/01/13/first-steps-with-s...