Jar文件中的变量替换后会将“–”去掉 – Bash

huangapple go评论78阅读模式
英文:

Jar file escapes "--" from substituted variable - Bash

问题

Code:

#!/bin/bash

MyVariable="--option arg1,arg2"

echo Variable output : $MyVariable
java -jar HelloInternet.jar "$MyVariable"

Expected results:

The jar file should recognize and use the value stored in the variable.

Actual results:

The jar file escapes "--" from "--option arg1,arg2", and interprets the variable without the "--".

Include any error messages:

Exception in thread "main" joptsimple.UnrecognizedOptionException: option arg1,arg2 is not a recognized option

Describe what you have tried:

I tried using ' ' instead of " " and vice versa without success.

英文:

Code:

#!/bin/bash

MyVariable="--option arg1,arg2"

echo Variable output : $MyVariable
java -jar HelloInternet.jar "$MyVariable"

Expected results:

The jar file should recognize and use the value stored in variable.

Actual results:

The jar file escapes "--" from "--option arg1,arg2" , and interprets the variable without the "--" .

Include any error messages:

Exception in thread "main" joptsimple.UnrecognizedOptionException: option arg1,arg2 is not a recognized option

Describe what you have tried:

I tried using ' ' instead of " " and vice versa without success.

答案1

得分: 0

使用数组当你需要一个数组:

MyVariable=(--option arg1,arg2)

java -jar HelloInternet.jar "${MyVariable[@]}"
英文:

Use an array when you need an array:

MyVariable=(--option arg1,arg2)

java -jar HelloInternet.jar "${MyVariable[@]}"

huangapple
  • 本文由 发表于 2020年10月21日 23:20:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64466748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定