简体   繁体   中英

Get return value from shell script in mule

Is there a way to get return value of a shell script in mule payload. when I am trying to get the value it returns me "java.lang.UNIXProcess@d58c32b".

I am new to this is there any way I can get value from the object.

I created a sample shell script

is_admin() {
return 1
}

test()
{
if is_admin;
then echo 0
else
echo 1
fi
}

test;

And below is the flow I am using to call this shell script:

<flow name="pythontestFlow">
    <http:listener config-ref="HTTP_Listener_Configuration1" path="/" doc:name="HTTP"/>
    <scripting:component doc:name="Script">
        <scripting:script engine="Groovy"><![CDATA[def command="/home/integration/scriptest/test.sh"
command.execute()]]></scripting:script>
       </scripting:component>
      </flow>

Thanks

To obtain the output messages of the execution , you have to print this in the script (with text function) and assign this to the payload:

In Groovy:

payload = "/home/integration/scriptest/test.sh".execute().text

Your flow:

<flow name="pythontestFlow">
    <http:listener config-ref="HTTP_Listener_Configuration1" path="/" doc:name="HTTP"/>
    <scripting:component doc:name="Script">
        <scripting:script engine="Groovy"><![CDATA[payload  = "/home/integration/scriptest/test.sh".execute().text]]></scripting:script>
    </scripting:component>
    <logger level="INFO" doc:name="Logger" message="#[payload]"/>
</flow>

You can also make it in java:

<component class="com.mulesoft.CommandExec" doc:name="Java"/>

Java class:

public class CommandExec implements Callable{

 @Override
 public Object onCall(MuleEventContext eventContext) throws Exception {

    Runtime rt = Runtime.getRuntime();

    Process proc = rt.exec("/your_path/test.sh");
    int returnCode = proc.waitFor(); 

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

    StringBuffer finalResult = new StringBuffer();
    finalResult.append("results:" + "\n");

    String results = "";
    while ((results = stdInput.readLine()) != null) {
       finalResult.append(results + "\n");
    }

    finalResult.append(" errors:");
    String errors = "";
    while ((errors = stdError.readLine()) != null) {
         finalResult.append(errors + "\n");
    }

    return finalResult;
 }
}

If you want only the return code:

Groovy:

payload =  "/home/integration/scriptest/test.sh".execute().exitValue()

Java:

return returnCode;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM