1

I'm trying to run a set of shell commands using the Scala process builder. In Scala, I run the process builder like this:

val command : String = ... // loaded from file somewhere
val processBuilder = Process(command)
val exitCode : Integer = processBuilder.!

the commands are (ran one by one):

/usr/bin/R --slave --silent --file=test.R argval1 >> out     
/usr/bin/R --slave --silent --file=test.R argval2 >> out     
/usr/bin/R --slave --silent --file=test.R argval3 >> out     

These three shell commands above will work without exceptions but the out file is never created. Then the following final command fails:

awk 'n < $0 {n=$0}END{print n}' out > final

basically it picks the smallest element of file out and puts it in file final. The awk command will fail with the following error while running it in command line works fine:

awk: syntax error at source line 1
context is
 >>> ' <<< 
awk: bailing out at source line 1

2 Answers 2

1

Those redirects are done by shell, and you are not running shell. maybe this would work better for you:

val processBuilder = Process("sh" :: "-c" :: command :: Nil)

Mind you, the process package let you redirect input and output directly, like this:

val processBuilder = Process(Seq("/usr/bin/R", "--slave", "--silent", "--file=test.R", "argval1")) #> new java.io.File("out")

Here I'm replacing a string with a Seq because that is generally a safer than letting Scala simply partition commands and arguments with spaces, since it doesn't recognize quotes.

Sign up to request clarification or add additional context in comments.

2 Comments

The first part of your answer solves most of the problems. The only open point left is that the append ">>" is being interpreted as ">" redirect therefore the three first commands overwrite the previous instead of appending. Any ideas? then I will accept. :)
@GiovanniAzua That's not a Scala problem. The whole thing is being passed to shell, and I can't think of any reason for shell to do that. I suspect you are not executing the command line you think you are executing.
0

The first option won't help if you need to run commands with |.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.