本文介绍了Groovy执行shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Groovy将 execute
方法添加到 String
中,使执行的shell非常容易; printlnls.execute()。text
但是如果发生错误,则不会有结果输出。 是否有简单的方法来同时获得标准错误和标准?(除了创建一堆代码以外;创建两个线程来读取两个输入流,然后使用父流来等待它们完成然后将字符串转换回文本?)
有这样的事情会很高兴;
<$ p $
def x = shellDo(ls / tmp / NoFile)
printlnout:$ {x.out} err:$ {x.err}
解决方案好了,我自己解决了它;
def sout = new StringBuilder(),serr = new StringBuilder()
def proc ='ls /badDir'.execute()
proc.consumeProcessOutput (sout,serr)
proc.waitForOrKill(1000)
printlnout> $ sout err> $ serr
显示:
out>犯错> ls:无法访问/ badDir:没有这样的文件或目录
Groovy adds the execute
method to String
to make executing shells fairly easy;
println "ls".execute().text
but if an error happens, then there is no resulting output. Is there an easy way to get both the standard error and standard out? (other than creating a bunch of code to; create two threads to read both inputstreams, then using a parent stream to wait for them to complete then convert the strings back to text?)
It would be nice to have something like;
def x = shellDo("ls /tmp/NoFile")
println "out: ${x.out} err:${x.err}"
解决方案 Ok, solved it myself;
def sout = new StringBuilder(), serr = new StringBuilder()
def proc = 'ls /badDir'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println "out> $sout err> $serr"
displays:
out> err> ls: cannot access /badDir: No such file or directory
这篇关于Groovy执行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!