Scala Process - Capture Standard Out and Exit Code

后端 未结 6 2241
心在旅途
心在旅途 2020-12-13 06:21

I\'m working with the Scala scala.sys.process library.

I know that I can capture the exit code with ! and the output with !!

6条回答
  •  有刺的猬
    2020-12-13 06:59

    You can specify an output stream that catches the text:

    import sys.process._
    val os   = new java.io.ByteArrayOutputStream
    val code = ("volname" #> os).!
    os.close()
    val opt  = if (code == 0) Some(os.toString("UTF-8")) else None
    

提交回复
热议问题