Question

I'm trying to develop a bash build script for a Java project that will be run on Ubuntu and Fedora. Ubuntu uses the gcj compiler while Fedora uses IcedTea.

Both report their errors and warning in slightly different ways, and I want to ignore the warnings (I know, not generally a good idea, but some of the warnings are simply idiotic).

For gcj, I want to run:

javac *.java 2>&1 | grep -A 4 "error:"

but for IcedTea, I want to run:

javac *.java 2>&1 | grep -A 4 "error:\|errors\|.java:"

I'm still new to bash, so how would I write an if statement that would run one versus the other based upon the javac version?

Was it helpful?

Solution

Assuming your java and javac binaries match, and that icedtea is the special case.

#!/bin/bash

ERROR="error:"

java -version 2>&1 | grep -i icedtea > /dev/null
if [ $? -eq 0 ]; then
   ERROR="error:\|errors\|.java:"
fi

javac *.java 2>&1 | grep -A 4 $ERROR

On my system, icedtea and sun have the same output for "javac -version", but not for "java -version".

OTHER TIPS

Writing Java build scripts in bash (or any other shell language) has a number of problems:

  • scripts tend to be non-portable due to shell differences, different command locations, incompatible command options and so on ... even if you try to make the portable.

  • scripts cannot cope with dependencies (or at least not easily)

  • scripts cannot cope with recompiling only stuff that has changed

Instead, I suggest that you write a "build.xml" file and use the Ant build tool. Ant has the advantage of running on any build platform that runs Java, and of taking care of the vast majority of platform differences. It is sort of like a better "Make" designed specifically for building Java.

    #!/bin/sh

    JAVAC_VERSION="`java -version 2>&1 /dev/null | awk '/IcedTea/ {print $4}' | sed -e 's/[\(0-9]//g'`"
    ICEDTEA="IcedTea"

    if [ ${JAVAC_VERSION} = ${ICEDTEA} ]; then
        javac *.java 2>&1 | grep -A 4 "error:\|errors\|.java:"
    else
        javac *.java 2>&1 | grep -A 4 "error:"
    fi

    exit 0

That should do it - if i understood your question correctly. How you get the version - im not quite sure of, but if my javac -version is incorrect just change it accordingly to your needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top