0

just something that i can't seem to figure it out. Begginner bash scripter here

I wrote a script to accept command line parameters that are later passed to maven

Here's is the working version of the script:

#!/bin/bash -x

# deploys the application and runs acceptance tests against it
if [ -n "$1" ]; then
#Executes acceptance tests containing the word $1 - eg: ats paymill, will only run test scenarios with the paymill word
    echo mvn clean verify -Pacceptance.test -Dcucumber.options=\"-n $1\"
    eval $(echo mvn clean verify -Pacceptance.test -Dcucumber.options=\"-n $1\")
else
    mvn clean verify -Pacceptance.test;
fi

But I would expect that this would work:

#!/bin/bash -x

# deploys the application and runs acceptance tests against it
if [ -n "$1" ]; then
#Executes acceptance tests containing the word $1 - eg: ats paymill, will only run test scenarios with the paymill word
    echo mvn clean verify -Pacceptance.test -Dcucumber.options=\"-n $1\"
    mvn clean verify -Pacceptance.test -Dcucumber.options=\"-n $1\"
else
    mvn clean verify -Pacceptance.test;
fi

But the execution of the last script (with the -x flag) gives me the following output

+ '[' -n paymill ']'
+ echo mvn clean verify -Pacceptance.test '-Dcucumber.options="-n' 'paymill"'
mvn clean verify -Pacceptance.test -Dcucumber.options="-n paymill"
+ mvn clean verify -Pacceptance.test '-Dcucumber.options="-n' 'paymill"'

I can see the the result of the echo command is the one i expect, but if i try to run that instead of echoing it does not work.

Where does those quotes come from around the -Dcucumber.options and between the -n and the paymill words?

Thanks for any help, just can't figure it out this one

1
  • 1
    Why did you escape the double-quotes with back-slashes? This stops the quotes from having any special meaning, so the space becomes a parameter separator, as the shell expansion shows. If you want the quotes in the mvn run string, you should escape the space as well, to stop it from acting as a separator.
    – AFH
    Dec 3 '14 at 11:27
1

The space after -n is not quoted nor backslashed, so bash performs word splitting on it. To prevent that, quote or backslash it. Once you do so, you can remove the double quotes, as the whole parameter is one word now:

mvn clean verify -Pacceptance.test -Dcucumber.options=-n\ $1
# or
mvn clean verify -Pacceptance.test -Dcucumber.options=-n' '$1

If the argument can contain whitespace, you should also consider doublequoting it.

mvn clean verify -Pacceptance.test -Dcucumber.options="-n $1"
13
  • Good point about spaces in $1: maybe the neatest solution is "\"-n $1\"". This makes sure that the whole string is a single parameter with quotes included in the expansion.
    – AFH
    Dec 3 '14 at 11:32
  • I thought about escaping the space but still it does not work: output with -x + mvn clean verify -Pacceptance.test '-Dcucumber.options="-n paymill"' There's still those single quotes around the -Dcucumber.options Dec 3 '14 at 11:42
  • @PedroGarciaMota: They are needed as they quote the space.
    – choroba
    Dec 3 '14 at 11:43
  • yes but i don't want them, they are most likely the cause of the script not running. If i use any of the options you gave me the maven execution crashes - java.lang.IllegalArgumentException: Not a file or directory: /Users/pedrompg/Documents/quandoo/checkout_api/checkout-acceptance-test/"-n Dec 3 '14 at 12:50
  • @PedroGarciaMota: Please, use ` to quote code and error samples. Also, please show the command line that works for you.
    – choroba
    Dec 3 '14 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.