0

I have script which runs java binary and want to pass complex command which will be executed when OutOfMemoryException happens. Problem is more general and is not related to OOME. How can I put commands in variable and pass it to other script which will execute it later? This is the code I'm trying to execute. I tried different options of escaping, but without any result. During execution shell tries to split JAVA_OPTS on spaces, no matter how I escape it. #!/bin/sh

JAVA_OPTS="-Xmx10m"
JAVA_OPTS="$JAVA_OPTS \"-XX:OnOutOfMemoryError=echo 'Ups'\""

set -x
java $JAVA_OPTS TestMemory

I found this:BashFAQ 050, but none of covered cases applies on me. I can't put this code in a function as JVM process will have no access to it. When I put the whole command as one line, it works well but my use case is a little bit different and I can't modify the last line. JAVA_OPTS should be constructed somewhere else and then used as shown in the sample code.

DaTval
  • 316
  • 1
  • 6
  • 14
  • Quote it: `java "$JAVA_OPTS" TestMemory` – anubhava Nov 03 '14 at 19:02
  • Thank you for you response. Unfortunately I can't modify the last line as it's in a different file and I don't have a control of it. Also adding quotes doesn't work when JAVA_OPTS contain something else also. Please see the modified example. – DaTval Nov 03 '14 at 19:14
  • 1
    As that BashFAQ entry indicates you *cannot* get complicated quoting through as a string variable. It cannot be done. You can try to use `eval` to handle just the right amount of quote removal but that is playing with fire. – Etan Reisner Nov 03 '14 at 19:17
  • With quotes it should still work, if you can provide a code to reproduce the problem then we can look into this otherwise it is very difficult. – anubhava Nov 03 '14 at 19:18
  • 2
    Use a script instead of an inline command for that. That way you can avoid needing to quote/escape spaces. – Etan Reisner Nov 03 '14 at 19:18
  • @anubhava - Thank you, the code I'm working right now is exactly what I put in the example. To understand full situation you can see my other question [link](http://stackoverflow.com/questions/26684777/restarting-process-on-xxonoutofmemoryerror-using-shell-script-gives-unrecogni/26684874#26684874), but it seems more complicated. – DaTval Nov 03 '14 at 19:38
  • @EtanReisner - I guess this is the only solution, it will make project more complicated but neither I see other solution. Thank you – DaTval Nov 03 '14 at 19:39

2 Answers2

0

The only sane way to do this is with bash arrays (or ksh/zsh)

JAVA_OPTS=( "-Xmx10m" )
JAVA_OPTS+=( "-XX:OnOutOfMemoryError=echo 'Ups'" )

set -x
java "${JAVA_OPTS[@]}" TestMemory
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Thank you for your comment. This really does work and probably will be helpful answer for lots of people. Unfortunately I can't modify that last line. JAVA_OPTS is constructed in other file and I can modify only that one. – DaTval Nov 04 '14 at 02:10
-1

try this

JAVA_OPTS="-Xmx10m"
read JAVA_OPTS<<eod
$JAVA_OPTS "-XX:OnOutOfMemoryError=echo 'Ups'
eod

set -x
java $JAVA_OPTS TestMemory
# you may need to change it slightly
michael501
  • 1,452
  • 9
  • 18
  • Thank you, sadly it does the same. Error is: Error: Could not find or load main class "-XX:OnOutOfMemoryError=echo – DaTval Nov 04 '14 at 02:05