urug urug - 6 months ago 32
Scala Question

executing unix commands from scala script

I am not clear on best practice to execute unix commands from my scala based script.

A typical scala script is here

exec scala "$0" "$0" "$@"

println("args length is ", args.length)

### want to list all files in the underlying folder
val resultSet = "ls -la"????

I checked a number of posts but nothing clearly outlines a working script which can execute a unix command from scala script and perform some transformations.


Here's a simple script to get you started.

#!/usr/bin/env scala
import scala.sys.process._

// args is auto-populated
println("arg len = " + args.length)

// get listing of current files
val files: Array[String] = "ls -a".!!.split("\n")
println("5th fiile is " + files(4))


Study the process package of the Standard Library for more on capturing process output, process exit status, pipelining, etc.