0

Suppose in a Linux shell script, I have WHATIF_POLLING_SCHEDULER="0 * * ? * *". Now I do: ESCAPED_WHATIF_POLLING_SCHEDULER=\"${WHATIF_POLLING_SCHEDULER}\"

CRON1="-Dwhatif.polling.scheduler=$ESCAPED_WHATIF_POLLING_SCHEDULER"

Finally, I have to do java -jar $MY_JAR and pass this cron paramater in it. So, when I do java "$CRON1" -jar $MY_JAR I am getting error like 'invalid value in cron parser'

While if I simply substitute the value java -Dwhatif.polling.scheduler="0 * * ? * *" -jar $MY_JAR it works fine. I need to pass the CRON1 variable correctly in Java command.

1
  • It's not clear what you pretend. Is it possible that you rephrase yourself to clarify? Commented Feb 13 at 14:42

2 Answers 2

3

I'm not sure what you are trying to do with ESCAPED_WHATIF_POLLING_SCHEDULER=\"${WHATIF_POLLING_SCHEDULER}\".

Note that java -Dwhatif.polling.scheduler="0 * * ? * *" -jar "$MY_JAR" does not really contain quotes in the value of the parameter. The quotes are part of the shell syntax.

The following commands are 100% identical from the viewpoint of the shell:

  • java "-Dwhatif.polling.scheduler=0 * * ? * *" -jar "$MY_JAR"
  • java -D"whatif.polling.scheduler"="0 * * ? * *" -jar "$MY_JAR"
  • java -D"whatif.polling.scheduler=0 * * ? * *" -jar "$MY_JAR"
  • java -Dwhatif.polling.scheduler=0" ""*"" ""*"" ""?"" ""*"" "*" -jar "$MY_JAR"
  • java -Dwhatif.polling.scheduler=0\ \*\ \*\ \?\ \*\ \* -jar "$MY_JAR"
  • java -Dwhatif.polling.scheduler='0 * * ? * *' -jar "$MY_JAR"
  • java -Dwhatif.polling."scheduler=0 * * ? * *" -jar "$MY_JAR"

So the command that you executed is equivalent to:

java -Dwhatif.polling.scheduler='"0 * * ? * *"' -jar "$MY_JAR"

You actually want to run:

WHATIF_POLLING_SCHEDULER='0 * * ? * *'
java -Dwhatif.polling.scheduler="$WHATIF_POLLING_SCHEDULER" -jar "$MY_JAR"

or maybe:

WHATIF_POLLING_SCHEDULER='0 * * ? * *'
CRON1="-Dwhatif.polling.scheduler=$WHATIF_POLLING_SCHEDULER"
java "$CRON1" -jar "$MY_JAR"

There's no need to embed quotes in your value, because the quotes are not actually part of the value.

1

The variable CRON1 contains the string -Dwhatif.polling.scheduler="0 * * ? * *" and this is what Java gets as first parameter, while when you type the version of your code which works fine, Java sees as first parameter the string -Dwhatif.polling.scheduler=0 * * ? * *.

I guess that Java is simply upset by the double quotes in your parameter. You are adding them when calculating ESCAPED_WHATIF_POLLING_SCHEDULER, so you should have done instead

 CRON1="-Dwhatif.polling.scheduler=$WHATIF_POLLING_SCHEDULER"

or alternatively remove the double quotes from the content of your CRON1 variable (i.e. pass

"${CRON1//'"'//}" 

to java)

Not the answer you're looking for? Browse other questions tagged or ask your own question.