You are not logged in.

#1 2009-01-18 17:55:59

JT
Member
Registered: 2009-01-16
Posts: 21

First bash script: Java compiler

Hello all,

After a year of linux, I've officially converted my desktop to Arch (best idea of 2008). I've switched all my Java coding from JCreator to Vim and command line operations.

I read everything I could find about bash and created this:

#!/bin/bash

COMPILE=""
echo
for file in *.java
do
    CLASS=`echo ${file%.java}`.class
    if [ ! -e $CLASS ] || [ $file -nt $CLASS ] ; then
        echo " > $file"
        COMPILE+=" $file"
    fi
done

if [ -n "$END" ] ; then
    echo
    echo "Compiling files . . ."
    javac $COMPILE
else
    echo "No compililation needed"
fi
echo

java GUI

I intend to compile only recently edited *.java files. It first checks for a *.class file, then checks the file's mtime. I ended the script with "java GUI" to run and test my project.

My questions is:
How do I check javac output for errors? I don't want to run "java GUI" if errors exist.

I know that I can save the sterr output with:

javac *.java 2> error.log

or

javac -Xstdout error.log *.java

So, how do I add a check for errors?
Could it be done without creating a log file?

Thanks in advance,
JT

Offline

#2 2009-01-19 02:10:24

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: First bash script: Java compiler

What's wrong with Ant?

Offline

#3 2009-01-19 03:55:28

pointone
Wiki Admin
From: Waterloo, ON
Registered: 2008-02-21
Posts: 379

Re: First bash script: Java compiler

In a Bash script, $? expands to the exit code of the last command. The convention is to exit 0 for success; any other number indicates some error.

some_command_here

if [ $? -eq 0 ]; then
    echo "Exited successfully!"
else
    echo "OMFG! ERROR!"
fi

M*cr*s*ft: Who needs quality when you have marketing?

Offline

#4 2009-01-19 17:30:51

JT
Member
Registered: 2009-01-16
Posts: 21

Re: First bash script: Java compiler

Perfect, thank you.

Offline

Board footer

Powered by FluxBB