在Scala中作为子进程调用外部命令

5
在Python中,如果我想将外部命令作为子进程调用,我会执行以下操作:
from subprocess import Popen, PIPE
cmd = ['cat', '-be']
out, err = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate("some input")

在Scala中,标准的方法是什么?使用Java的ProcessBuilder,我想到了以下方法,但它非常丑陋:

def communicate(cmd: List[String], input: Option[String] = None): (String, String) = {

    val command = new java.util.ArrayList[String]()
    cmd.foreach(command.add(_))

    val builder = new ProcessBuilder(command)
    val process = builder.start()

    val stdinWriter = new java.io.PrintWriter((new java.io.OutputStreamWriter(new java.io.BufferedOutputStream(process.getOutputStream()))), true);
    val stdoutReader = new java.io.BufferedReader(new java.io.InputStreamReader(process.getInputStream()))
    val stderrReader = new java.io.BufferedReader(new java.io.InputStreamReader(process.getErrorStream()))

    input.foreach(stdinWriter.write(_))
    stdinWriter.close()

    def read(reader: java.io.BufferedReader): String = {
        val out = new ListBuffer[String]
        var line: String = reader.readLine()
        while (line != null) {
            out += line
            line = reader.readLine()
        }
        return out.result.mkString("\n")
    }

    val stdout = read(stdoutReader)
    val stderr = read(stderrReader)

    stdoutReader.close()
    stderrReader.close()

    return (stdout, stderr)

}

val (catout, caterr) = communicate(List("cat", "-be"), Some("some input"))
val (pwdout, pwderr) = communicate(List("pwd"))

有没有Scala内置的更好的替代方案?
3个回答

11

太棒了。这正是我正在寻找的那种东西。 - dhg

1

使用OS-Lib库,您的代码可以编写为以下Scala代码:

@ val res = os.proc("cat", "-be").call(stdin = "some input")
res: os.CommandResult = CommandResult(0, List(Left(     1   some input)))

@ res.out.string
res3: String = "     1\tsome input"

@ res.err.string
res4: String = ""

0

你看过Apache commons exec http://commons.apache.org/exec/tutorial.html吗?

几年前我从Scala成功地使用过它。我找到了一些代码:

def quickRun(command: String, allowedExitCodes: Array[Int]): String = {
  val executor = new DefaultExecutor()
  val outputStream = new ByteArrayOutputStream()
  val errorStream = new ByteArrayOutputStream()

  val pumpStreamHandler = new PumpStreamHandler(outputStream, errorStream)
  executor.setStreamHandler(pumpStreamHandler)
  executor.setExitValues(allowedExitCodes)

  lazy val errorMsg = "Couldn't execute command: \"" + command + "\", errorStream: " + errorStream.toString()
  try {
    val exitValue = executor.execute(CommandLine.parse(command))
    if (executor.isFailure(exitValue)) {
      log.error(errorMsg)
      throw new CommandLineException(errorMsg)
    } else {
      return outputStream.toString()
    }
  } catch {
    case e: ExecuteException =>
      log.error(errorMsg)
      throw new CommandLineException(errorMsg)
    case e: IOException => throw new CommandLineException(errorMsg)
  }
}

但是 scala.sys.process 看起来更好,如果可能的话,我认为你应该使用它...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接