1

When I run Process(command), the output of the command is printed out to terminal, but I only want the exitValue().

Is there a way to suppress the output displayed on terminal? I just find sometime the command processed could print a huge output result on terminal. Thanks a lot.

scala> import scala.sys.process._
import scala.sys.process._

scala> Process("ls").run().exitValue()
README.md
Vagrantfile
bin
box
config
package.sh
vendor
res6: Int = 0
keypoint
  • 2,268
  • 4
  • 31
  • 59
  • 1
    I think I still like my answer to http://stackoverflow.com/questions/15411728/scala-process-capture-standard-out-and-exit-code but Process is too complex to remember right. Also http://stackoverflow.com/questions/22927138/scala-get-list-of-directories-from-process – som-snytt Aug 27 '16 at 03:01

2 Answers2

5

Use the run() overload that takes a ProcessLogger. Use a ProcessLogger that ignores strings passed to out and err:

Process("ls").run(ProcessLogger(_ => ())).exitValue()
danielnixon
  • 4,178
  • 1
  • 27
  • 39
  • 1
    I'm too lazy to figure out whether your answer is better than the answers linked from my other comment. I guess that's why I don't use Process? It's very complex compared to sh. But I'll +1 b/c I'm too lazy not to. – som-snytt Aug 27 '16 at 03:04
  • maybe this one is more concise, less code :P, thank you all the same – keypoint Aug 27 '16 at 03:46
2

Simple one line answer

import scala.sys.process._

"ls" ! ProcessLogger(_ => ())

or

import scala.sys.process._

"ls" ! ProcessLogger(a => (), b  => ())

Gives only the exit value

output

scala> import scala.sys.process._
scala> "ls" ! ProcessLogger(a => (), b  => ())
res11: Int = 0
Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40
  • I like this. I didn't know about those factory methods on the ProcessLogger object. There's another overload that takes a single function, so you could simply use `"ls" ! ProcessLogger(_ => ())`. I've updated my answer. Note that those `Unit`s don't do what you might think they do (pass `-Ywarn-value-discard` to scalac and see what happens). – danielnixon Aug 27 '16 at 09:13