Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala sys.process nonzero exit values

Tags:

process

scala

On comparing different files

import sys.process._"
"diff data.txt myFile.txt" !

the exit code proves nonzero as expected,

< 1
---
> 123
res1: Int = 1

However on trying to gather the actual differences

val d = "diff data.txt myFile.txt" !!

java.lang.RuntimeException: Nonzero exit value: 1
  at scala.sys.package$.error(package.scala:27)
  at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.slurp(ProcessBuilderImpl.scala:132)
  at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.$bang$bang(ProcessBuilderImpl.scala:102)
  ... 33 elided

To ask how to fetch the differences into a String (given small files) ?

like image 509
elm Avatar asked Mar 17 '23 09:03

elm


1 Answers

I think that you need to use the lineStream_! method, and not the !. With the line stream which is the output of the diff command, you can do whatever you want: print it, convert it to list, to array, or whatever else you need.

val st = "diff input.txt output.txt" lineStream_!
// now st is a Stream[String]
println(st.mkString("\n"))

EDIT: I believe that method was called before lines_!. Now it's deprecated. Just in case you are using an old Scala version.

like image 65
ale64bit Avatar answered Mar 24 '23 02:03

ale64bit