简体   繁体   中英

Redirect output of unit function/method in scala as standard in for linux mail command

Lets say I have a unit function/method that such as the following:

def PrintOrders(): Unit = {
   println("stuff")
   println("things")
   anotherUnitFunctionThatPrintsThings()
    ...
  }

Is there a way for me to redirect the output produced in this unit command as an argument to the process or processbuilder libraries to execute the linux mail command using this output?

edit: just to be safe, I want this done from within the scala program and not using a bash script.

You can collect the output using your own PrintStream:

val s = new java.io.ByteArrayOutputStream
Console.withOut(new PrintStream(s)) {
  PrintOrders()
}
val output = s.toString("UTF-8")

Then you can use that as a string for whatever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM