I am using Groovy to execute commands on my Linux box and get back the output, but I am not able to use |
pipes somehow (I think) or maybe it is not waiting for
You cannot do pipes or redirects using String.execute()
. This doesn't work in Java, so it doesn't work in Groovy either...
You can use Process.pipeTo
with Groovy to simplify things:
Process proca = 'uname -a'.execute()
Process procb = 'awk {print\$2}'.execute()
(proca | procb).text
A more generic version could be:
String process = 'uname -a | awk {print\$2}'
// Split the string into sections based on |
// And pipe the results together
Process result = process.tokenize( '|' ).inject( null ) { p, c ->
if( p )
p | c.execute()
else
c.execute()
}
// Print out the output and error streams
result.waitForProcessOutput( System.out, System.out )