Zofren
Zofren

Reputation: 1190

Losing java main args when using getopts in launch script

I have a script with 5 mandatory parameters (5 paths) and 3 options (-d for debug, -l for log4j override, -s for another override).

I'm managing it with getopts. The following script is simplified :

LOG4J_FILE=$DEFAULT_LOG4J_FILE
S_FILE=$DEFAULT_S_FILE
ECLIPSE_PROPS=

while getopts "l:s:d" flag; do
case "$flag" in
   l) LOG4J_FILE="$OPTARG";;
   s) S_FILE="$OPTARG";;
   d) ECLIPSE_PROPS="-Xdebug ...";;
   :) usage;;
   ?) usage;;
esac
done

shift $((OPTIND-1))
OPTIND=1

...

echo_and_eval $JAVA $ECLIPSE_PROPS -Dlog4.configuration=$LOG4J_FILE -Ds.file=$S_FILE -cp $CLASSPATH $MAIN_CLASS $ARGS

If I just put the 5 parameters, it works. If I add one or two optional with parameters (l or s), it works. If I add the -d option, I have no args in the Java main method.

Any clue ? This is driving me crazy.

Upvotes: 2

Views: 124

Answers (2)

Mark Reed
Mark Reed

Reputation: 95252

Simply using echo to output the command line isn't going to show you how it gets parsed into individual arguments. foo 'bar baz' and foo bar baz are two very different commands, but they look the same when echoed.

Clearly, something in the actual value of the ECLIPSE_PROPS variable (that is, the argument to -d) is preventing the later arguments from being passed to the Java main method. If you supplied the actual code and actual values, we might be able to help you determine what that is.

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246799

It's OPTARG not OPTARGS -- http://www.gnu.org/software/bash/manual/bashref.html#index-getopts

l) LOG4J_FILE="$OPTARG";;

I would encourage you to get into the habit of quoting ALL your variables, that will protect any that contain whitespace or globbing chars:

java "$ECLIPSE_PROPS" -Dlog4.configuration="$LOG4J_FILE" -Ds.file="$S_FILE" -cp "$CLASSPATH" Main "$1" "$2" "$3" "$4" "$5"

Upvotes: 1

Related Questions