7

您可以使用 .text获取整个输出流:

def process = "ls -l".execute()
println "Found text ${process.text}"

是否有一个简洁的等价物来获取错误流?

4

2 回答 2

10

您可以使用waitForProcessOutput需要两个附加项(此处的文档

def process = "ls -l".execute()
def (output, error) = new StringWriter().with { o -> // For the output
  new StringWriter().with { e ->                     // For the error stream
    process.waitForProcessOutput( o, e )
    [ o, e ]*.toString()                             // Return them both
  }
}
// And print them out...
println "OUT: $output"
println "ERR: $error"
于 2012-05-21T16:10:07.807 回答
3

根据 tim_yates 的回答,我在 Jenkins 上进行了尝试,发现这个问题有多个分配:https ://issues.jenkins-ci.org/browse/JENKINS-45575

所以这很有效,而且也很简洁:

def process = "ls -l".execute()
def output = new StringWriter(), error = new StringWriter()
process.waitForProcessOutput(output, error)
println "exit value=${process.exitValue()}"
println "OUT: $output"
println "ERR: $error"
于 2017-10-26T14:35:54.163 回答