简体   繁体   English

Bash脚本从文件中读取并执行命令

[英]Bash script to read from a file and execute commands

I want a bash script that reads the java arguments from a file and executes java with them 我想要一个bash脚本,它从文件中读取java参数并用它们执行java

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

The problem is that it does not work very well with this jvm-args file: 问题是它对这个jvm-args文件不能很好地工作:

-Xms128m -Xmx512m -Dhostname=$(hostname)

The command hostname is not executed and replaced. 命令hostname不会被执行和替换。

How can I get the hostname command executed? 如何执行hostname命令? so that I get: 这样我得到:

exec java -Xms128m -Xmx512m -Dhostname=MyMachine

Thanks. 谢谢。

Use eval to evaluate your runtime variables. 使用eval评估运行时变量。 Like this (not tested): 像这样(未经测试):

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

Store the whole command in file, for example, run.sh 将整个命令存储在文件中,例如run.sh

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

If you want current bash process to be terminated, call with . run.sh 如果您希望终止当前的bash进程,请使用. run.sh . run.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM