【发布时间】:2012-07-20 12:49:51
【问题描述】:
我有一个 Groovy 脚本,它通过一个目录递归查找 .png 文件,并在每个目录上调用 pngquant(一个命令行实用程序)。 pngquant 的输出应该打印在终端上。相关代码为:
def command = "pngquant -f -ext .png"
root.eachFileRecurse(groovy.io.FileType.FILES) {File file ->
if (file.name.endsWith('.png')) {
println "Compressing file: $file"
def imgCommand = "$command $file.absolutePath"
Process pngquantCmd = imgCommand.execute()
pngquantCmd.consumeProcessOutput(System.out, System.err)
}
}
脚本运行良好,但是一旦处理完所有文件,stout 似乎仍在被重定向,因为命令提示符永远不会出现,除非我使用 Ctrl + C。我是否需要以某种方式“撤消”
pngquantCmd.consumeProcessOutput(System.out, System.err)
或者有没有更好的方法将此进程的输出重定向到控制台?我想我可以通过添加System.exit(0) 来解决这个问题,但这似乎不是正确的解决方案。该问题仅出现在 Linux 上。
【问题讨论】:
-
waitForProcessOutput( System.out, System.err )工作得更好吗? -
@tim_yates 是的,它工作得更好,非常感谢!如果您将其发布为答案,我会接受它
标签: groovy