Search code examples
linuxvariablesjenkinsenvironmentsingle-quotes

How to correctly parse build parameters in Jenkins running on Linux when receiving a string with spaces and double quotes?


I configured a parametrized Jenkins Job build which takes the following argument called "testcases":

-t "Can Get Fake Name" -t "Can call Password"

This is a string that has to be passed via command line to the robot framework executable.

Jenkins is running on Linux (Centos 7) and this is the build script I wrote to achieve my target:

robot "${testcases}" myrobottest.robot

I have found the ${testcases} suggestion here which is partially solving my issue.

The result command on Jenkins is as it follows:

+ robot '-t "Can Get Fake Name" -t "Can call Password"' myrobottest.robot
[ ERROR ] Suite 'myrobottest' contains no tests named ' "Can Get Fake Name" -t "Can call Password"'.

The problem here are the trailing single-quotes that I don't know how to remove (or to not produce).

Before using the ${testcases} notation I also tried to use the environment variable like $testcases but it produces something with many useless single-quotes.

Any suggestion please?

I expect the generated command line to be

robot -t "Can Get Fake Name" -t "Can call Password" myrobottest.robot

but actual output is:

robot '-t "Can Get Fake Name" -t "Can call Password"' myrobottest.robot

EDIT

I did one more attempt. If I change the script line as below:

eval echo robot \${testcases} docker-robot-framework.robot

I get the following console output.

+ eval echo robot '${testcases}' myrobottest.robot
++ echo robot -t '"Can' Get Fake 'Name"' myrobottest.robot
robot -t "Can Get Fake Name" myrobottest.robot

I think I'm close to the solution, because I only have to execute such eval expression (that is correct by the way).


Solution

  • I solved doing the following way:

    /bin/sh -c "robot $testcases myrobottest.robot"
    

    Actually, the resulting command line that is executed is correct now, and working:

    /bin/sh -c 'robot -t "Can Get Fake Name" -t "Can call Password" myrobottest.robot'