First steps with Scala, say goodbye to bash scripts…
playlatam.wordpress.com
playlatam.wordpress.com
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))%"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.
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.
apart from that, I came to a similar conclusion: http://playlatam.wordpress.com/2012/01/13/first-steps-with-s...
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)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))Type safety, much better type inference than Scala, and no JVM overhead.
#!/bin/sh
exec scala "$0" "$@"#!/usr/bin/env scala
as the first line instead. If there's some hack to support #!/bin/sh in the scala interpreter... ew.
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
#!/usr/bin/env scala
!#
it appears to work.