1

I want a bash script that reads the java arguments from a file and executes java with them

jvm_arguments=$(cat jvm-args)
exec java $jvm_arguments

The problem is that it does not work very well with this jvm-args file:

-Xms128m -Xmx512m -Dhostname=$(hostname)

The command hostname is not executed and replaced.

How can I get the hostname command executed? so that I get:

exec java -Xms128m -Xmx512m -Dhostname=MyMachine

Thanks.

2
  • 1
    Be careful with this. There is a reason why bash does not do this sort of thing automatically: it can be a gaping security hole. Commented Nov 5, 2014 at 22:55
  • InetAddress.getLocalHost().getHostName() might be another solution (download.java.net/jdk7/archive/b123/docs/api/java/net/…) Commented Nov 5, 2014 at 23:22

2 Answers 2

2

Use eval to evaluate your runtime variables. Like this (not tested):

jvm_arguments=$(cat jvm-args)
eval java $jvm_arguments
Sign up to request clarification or add additional context in comments.

1 Comment

If bash only, jvm_arguments=$(< jvm-args) will cause the shell to read the file rather than needing another cat process.
1

Store the whole command in file, for example, run.sh

exec java -Xms128m -Xmx512m -Dhostname=$(hostname) ...

If you want current bash process to be terminated, call with . run.sh

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.