Shell Scripting - pford68/groovy-examples GitHub Wiki
Invoking shell commands and scripts in Groovy is easy. Put the shell command in a string, then invoke the String's execute() method:
"ls -l ~/".execute()
The execute() returns a process, with has a text property that contains the output, as well as a `consumeProcessOutput()' method which you can use to process the output.
This method does not handle wildcards, however.
To print the output in Groovy, you can simply get the text property of the return value of execute() and print it:
println "ls -l".execute().text
In order to process the output, however, (i.e., do something), use the consumeProcessOutput(out, err) function of the process returned by execute(). The method takes two StringBuffer` arguments--one for the out stream and one for the err stream.
Example:
Process process = command.execute()
def out = new StringBuffer()
def err = new StringBuffer()
process.consumeProcessOutput( out, err )
process.waitFor()
if( out.size() > 0 ) println out
if( err.size() > 0 ) println err
You can then work with those streams.