Shellcheck suggestion for SC2806 giving me issue with JAVA_OPTS

79 Views Asked by At

I'm getting shellcheck suggestion SC2806 i.e. Double quote to prevent globbing and word splitting for $JAVA_OPTS.

When I apply that to my code java $JAVA_OPTS -Dhttps.proxyHost=%PROXY_HOST%

I get the 503 Error Back-end server busy error. The ec2 instance activity reports that the instance health check failed.

But when I don't use "" to $JAVA_OPTS, then it works and I get 200 OK response and everything works.

when I echoed $JAVA_OPTS it echoed as empty.

What could be the reason for this?

2

There are 2 best solutions below

2
Konrad Rudolph On BEST ANSWER

The other answer explains what causes the issue.

Here’s how to fix it: instead of passing $JAVA_OPTS (either quoted or unquoted) to java, pass ${JAVA_OPTS:+"$JAVA_OPTS"}. This will cause the parameter expansion only if it is non-empty. If $JAVA_OPTS is empty, no expansion will be passed to the command.

See the documentation for more information about the :+ expansion.

0
SK16 On

So if JAVA_HOME is not set (which it looks like it isn’t), then what bash expands the command to differs depending on how you provide it. For example: java $JAVA_HOME --version

expands to:

java --version

In this case, bash ends up passing in a single argument to java which will be --version (it ignores any extra spaces) and this works just fine.

However, contrast that to:

java "$JAVA_HOME" --version

this expands to:

java "" --version

So, in this case, bash will pass TWO arguments to java: an empty string and --version.

The first parameter to Java is supposed to be the class name to execute and you can see it complain if you try to run this command:

java "" --version

Error: Could not find or load main class  Caused by: java.lang.ClassNotFoundException:

So, TLDR is, that the quotes matter if JAVA_OPTS is not set to anything.