I use Groovy to execute commands in my LINUX block and return output, but I cannot use | pipes somehow (I think), or maybe he is not waiting for the completion of the command, whatβs wrong, or what am I missing in my code?
My Call Function
def test() { String result="N" HashMap<String,String> params = IntermediateResults.get("userparams") Map env=AppContext.get(AppCtxProperties.environmentVariables) def fClass = new GroovyClassLoader().parseClass( new File( 'plugins/infa9/Infa9CommandExecUtil.groovy' ) ) List<String> frows=["uname -a", "uname -a | awk '{print\$2}'", "uname -a | cut -d ' ' -f 2"] List<String> resultRows = fClass.newInstance().fetchCommandOutput( params, env, frows ) return result }
Infa9CommandExecUtil.groovy file contents Update added exitVal println
package infa9 import java.io.BufferedReader; public class Infa9CommandExecUtil { StringBuffer result public Infa9CommandExecUtil() { result = new StringBuffer() } public List<String> fetchCommandOutput( Map<String,String> params, Map env, List<String> rows ) { List<String> outputRows = new ArrayList<String>() try { for(item in rows) { String temp=item.toString() println "CMD:$temp" Process proc = Runtime.getRuntime().exec(temp); InputStream stdin = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(stdin); BufferedReader br = new BufferedReader(isr); String line = null; result = new StringBuffer() line=null int exitVal = proc.waitFor()
My Exit * Update added value exitVal *
CMD:uname -a OUTPUT:Linux estilo 2.6.18-128.el5
Update
Note I am using sh -c <command>
source share