Escaping single quotes in shell() steps

1,742 views
Skip to first unread message

Misty Stanley-Jones

unread,
Apr 28, 2015, 2:24:55 AM4/28/15
to job-dsl...@googlegroups.com
I am having a heck of a time. I have a string formatted like this:

def myString = "'value1' 'value2' 'value3' 'value4'"

I need to assign it as the contents of a Bash array in the shell() step, but I am really unsure how to do this. I've tried like this:
shell(
"""
#!/bin/bash
MY_LIST=(${myString})
""")


It looks right in the XML, but the job doesn't actually run. Instead I get an error: "Syntax error: "(" unexpected"

If I go into Configure and Save, without making any actual changes, Jenkins changes all the ' characters to ' and the script works thereafter. What's the right way for me to code this in the DSL so that the escape passes through properly? I've had lots of places where I've needed to escape double quotes and $ signs in the actual Bash script, but I haven't had to escape a whole string coming from Groovy into Bash.

Any ideas? I know this must be simple.

Patrick van Dissel

unread,
Apr 28, 2015, 2:40:28 AM4/28/15
to job-dsl...@googlegroups.com
The following should work:

shell("""\
#!/bin/bash
MY_LIST="${myString}"
""")

"""\ Means that the first newline will be dismissed, so the in the xml
you will only see the bash script without prefixing newline.
(see
http://docs.groovy-lang.org/latest/html/documentation/#_triple_single_quoted_string)

If you have this with indents, you can do:

shell("""\
#!/bin/bash
MY_LIST="${myString}"
""".stripIndent())

The stripIndent() will remove all indents as expected :)
(see
http://docs.groovy-lang.org/latest/html/gapi/org/codehaus/groovy/runtime/StringGroovyMethods.html#stripIndent%28java.lang.CharSequence%29)

Hope that helps you,
Patrick
> --
> You received this message because you are subscribed to the Google
> Groups "job-dsl-plugin" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to job-dsl-plugi...@googlegroups.com
> <mailto:job-dsl-plugi...@googlegroups.com>.
> To post to this group, send email to job-dsl...@googlegroups.com
> <mailto:job-dsl...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/job-dsl-plugin/8104b525-80c7-43ac-a58c-425efbcc7b58%40googlegroups.com
> <https://groups.google.com/d/msgid/job-dsl-plugin/8104b525-80c7-43ac-a58c-425efbcc7b58%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Misty

unread,
Apr 28, 2015, 2:48:30 AM4/28/15
to job-dsl...@googlegroups.com
Thanks, that might be working! If I understand it correctly, a line was added to the top of my script, so the #!/bin/bash was ignored and the shell was running using sh instead of bash. Using the """\ syntax seems to have done the trick.

I didn't need to do the stripindent() thing, but it's good to know. I have my Bash syntax un-dented compared to the Groovy, already.

--
You received this message because you are subscribed to a topic in the Google Groups "job-dsl-plugin" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/job-dsl-plugin/QEKT9qsgyyU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to job-dsl-plugi...@googlegroups.com.
To post to this group, send email to job-dsl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/553F2B5B.9050105%40gmail.com.
Reply all
Reply to author
Forward
0 new messages