您不能使用进行管道或重定向
String.execute()。这在Java中不起作用,因此在Groovy中也不起作用…
您可以
Process.pipeTo与Groovy一起使用以简化操作:
Process proca = 'uname -a'.execute()Process procb = 'awk {print$2}'.execute()(proca | procb).text更通用的版本可能是:
String process = 'uname -a | awk {print$2}'// Split the string into sections based on |// And pipe the results togetherProcess result = process.tokenize( '|' ).inject( null ) { p, c -> if( p ) p | c.execute() else c.execute()}// Print out the output and error streamsresult.waitForProcessOutput( System.out, System.out )


