用其他东西代替eval

时间:2019-04-23 23:02:39

标签: java linux bash eval

我正在尝试用其他方式替换代码中的eval。我目前拥有的东西正在工作:

#$1 command "command"
#$2 params "--source='Waiting b'"
fun_example(){
  eval java -Dfile.encoding="UTF-8" \  
  -Duser.language="en" -Dspring.profiles.active="x" -jar /x.jar \
  ${1} $(echo ${2} | sed "s|'|\"|g")
} 

我尝试了各种方法,但是没有任何效果。主要问题是应用程序以其他任何方式接收带有引号示例“ Waiting”的参数,并且如果我不输入任何“”,则它在有空格的情况下将被切断Waiting而不是Waiting b。我可能会更改它在应用程序内部删除引号,但我想避免这种情况。

如果我这样做,就会遇到上述问题。

java -Dfile.encoding="UTF-8" \  
-Duser.language="en" -Dspring.profiles.active="x" -jar /x.jar \
${1} $(echo ${2} | sed "s|'|\"|g")

谢谢。

更新:主要问题是我的命令存储在一个env变量中!

1 个答案:

答案 0 :(得分:0)

在经历了许多痛苦和@thatootherguy的一些有用建议之后,我最终使用了该解决方案

<div id="quizContainer">
  <p id="question"></p>
  <input id="opt1" name="option" type="radio"><label for="opt1" id="lbl_1"></label>
  <input id="opt2" name="option" type="radio" /><label for="opt2" id="lbl_2"></label>
  <input id="opt3" name="option" type="radio" /><label for="opt3" id="lbl_3"></label>
</div>
<br/>
<button id="nextButton" onclick="loadNextQuestion()">Next</button>
<br/>
<p id="progressText"></p>
<div id="bar-horzontal"></div>
<div id="result"></div>

希望这可以帮助某人。就像@thatootherguy所说的那样,目标是拥有一个参数数组,以便我们不会触发system(3)风格的语义