Home > other >  executing variable value as a command gives different result than executing directly in the shell
executing variable value as a command gives different result than executing directly in the shell

Time:09-17

i'm facing a issue with a mvn command i'm building as a variable value, and then executing.

If i execute the command direcly, as the echo of the variable value, it works perfectly. But if i execute the variable value, it gives me an error like one of the parameters is not inside double quotes.

I need to build the command cause the parameters depends on certain variable names that came from other scripts than run these.

These are the commands:

With this one, i store the maven command in a variable with some extra parameters if the $CLOUD value is true:

$ export CLOUD=true && export mvn_cmd="mvn clean test --batch-mode $(if [[ "$CLOUD" == "true" ]]; then echo -Ddevice=\"Samsung Galaxy J7 Prime\" -DnetworkLog=true; fi) -Dcucumber.filter.tags=\"@tmsLink=ID-16848\""

If i echo the command is perfectly fine:

$ echo $mvn_cmd
mvn clean test --batch-mode -Ddevice="Samsung Galaxy J7 Prime" -DnetworkLog=true -Dcucumber.filter.tags="@tmsLink=ID-16848"

if i run this with:

$ $mvn_cmd
...
[ERROR] Unknown lifecycle phase "Galaxy". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]

But if i copy and paste the output of the variable diretly in the terminal, it runs perfectly:

$ mvn clean test --batch-mode -Ddevice="Samsung Galaxy J7 Prime" -DnetworkLog=true -Dcucumber.filter.tags="@tmsLink=TUF-16848"
...
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ flow ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------

Any ideas? Thanks in advance!

CodePudding user response:

So, the problem is

  1. You're storing a command in a shell variable instead of a function, and
  2. You aren't quoting your shell variable, and
  3. You'd have to use eval or similar to execute it if quoted properly.

Try this instead (assuming you're using bash):

mvn_cmd() {
    local args
    [[ "$CLOUD" == "true" ]] && args='-Ddevice="Samsung Galaxy J7 Prime" -DnetworkLog=true'
    echo mvn clean test --batch-mode ${args?"$args"} -Dcucumber.filter.tags="@tmsLink=ID-16848"
}

$ mvn_cmd
mvn clean test --batch-mode -Dcucumber.filter.tags=@tmsLink=ID-16848

$ CLOUD='true'
$ mvn_cmd
mvn clean test --batch-mode -Ddevice="Samsung Galaxy J7 Prime" -DnetworkLog=true -Dcucumber.filter.tags=@tmsLink=ID-16848

Remove the echo when you're done testing it and ready to use it for real.

  • Related