user3492304
user3492304

Reputation: 163

How return query output back to shell script?

I am invoking ant from shell script. Ant in turn invokes java to run a SQL query. Query returns a flag value, either 0 or 1.

How to return query output (0/1) back to shell script?

Upvotes: 1

Views: 989

Answers (3)

Slav
Slav

Reputation: 27495

In your ANT file, configure the java task to write output to property called output. Then, if the value of output is not 0 we can FAIL the build, and pass value of output to shell. Else, the build will be SUCCESS and pass 0 to shell.

<java jar="..." outputproperty="output"/>
<!-- Exit with non-zero exit code -->
<fail message="Output returned non-zero" status="${output}">
    <condition>
        <not>
            <matches pattern="0" string="{output}"/>
        </not>      
    </condition>
</fail>
<!-- Else ANT exits with 0 exit code -->

Now in your SHELL script, capture the exit code with $?

# Your ANT execution
ant -f build.xml 
# Capture exit code into variable $exitcode
exitcode=$?
# Decide what to do with $exitcode
if [ $exitcode -eq 0 ]; then
    echo "ANT returned with 0"
else
    echo "ANT returned with $exitcode"
fi

Upvotes: 0

Boris Brodski
Boris Brodski

Reputation: 8715

You can return value from the java to the ant using the outputproperty as @VirtualTroll presented. Then in ant I would write the variable to the temporary file:

<java jar="..." outputproperty="output"/>
<echo file="/tmp/query_result">${output}</echo>

Now in the bash script you should first check the error code of the ant and the existence of the file and then read the content.

Upvotes: 1

VirtualTroll
VirtualTroll

Reputation: 3091

The java task from ant has a special parameter "outputproperty" to capture the output of the java task.

You only need to set the parameter in your java task

 <java jar="..." outputproperty="output"/>
 <echo>The output is ${output}</echo>

Then you can do whatever you want with the parameter value.

Upvotes: 0

Related Questions