从mule中的shell脚本中获取返回值

时间:2015-12-22 22:46:39

标签: java shell mule

有没有办法在mule有效负载中获取shell脚本的返回值。 当我想要获得它的价值时,它会返回我" java.lang.UNIXProcess@d58c32b"。

我是新来的,有什么方法可以从对象中获得价值。

我创建了一个示例shell脚本

is_admin() {
return 1
}

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

test;

以下是我用来调用这个shell脚本的流程:

<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>

由于

1 个答案:

答案 0 :(得分:0)

要获取执行的输出消息,您必须在脚本中打印(带有文本功能)并将其分配给有效负载:

在Groovy中:

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

你的流程:

<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>

你也可以在java中创建它:

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

Java类:

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;
 }
}

如果您只想要返回代码:

Groovy的:

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

爪哇:

return returnCode;