Matthias Meid
Matthias Meid

Reputation: 12513

bash vs. scala.sys.process.Process with Command Line Arguments

I want to spawn a process (SBT for that matter) from Scala, along with two SBT-commands passed as arguments. I made an sbt.sh shell script (simplified version of Manual Installation in sbt Documentation):

java -jar /home/bob/sbt/sbt/bin/sbt-launch.jar "$@"

If works fine when I invoke the script from a bash prompt in Ubuntu (/home/bob/workingdir being the working directory):

~/sbt.sh "project Foo" "run"

However, I'd like to invoke it in a Scala program with the following piece of code:

val pseq = Seq("/home/bob/sbt.sh", "\"project Foo\"", "\"run\"")
val pb = scala.sys.process.Process(pseq, new java.io.File("/home/bob/workingdir"))
pb.!

SBT starts and loads normally, then fails with the following error output:

[error] Expected key
[error] "project Foo"
[error] ^

It works fine on Windows 8, but fails on Ubuntu 12.10. My pseq used to be an ordinary String with the full bash command. I turned into a Seq in order to get the argument splitting explicit (rather than at any space, even within quotes).

Can anybody point out what's the difference between the two invocations, or what else I'm doing wrong?

Upvotes: 3

Views: 1849

Answers (1)

Matthias Meid
Matthias Meid

Reputation: 12513

I came across the solution. Turned out to be trivial, but worth sharing anyway:

// no \"-quoting of passed SBT commands here!
val pseq = Seq("/home/bob/sbt.sh", "project Foo", "run")
val pb = scala.sys.process.Process(pseq, new java.io.File("/home/bob/workingdir"))
pb.!

Neat, and works on both Windows and Linux. Using /home/bob/sbt.sh "project Foo" "run" as a command string is rather unelegant on Windows and does not work on Linux.

Upvotes: 5

Related Questions