0

I understand that the basic format is something like echo | sed '../../..'

I've been accessing the Jenkins params variable as such:

sh label: '', script: ' echo "${params}" | sed \'s/\\([[:alnum:]_][[:alnum:]_]*\\)/"\\1"/g\''

the "sh label" and "script" elements are from the pipeline syntax snippet generator that Jenkins provides.

Yet as an output the echo just outputs a blank line, and if I echo separately after the statement, the params variable is unchanged.

I know my regex is working perfectly as I've tested it directly on multiple strings.

I figure my error lies somewhere with how I'm accessing the reserved Jenkins params object, and that sed isn't accessing it as a string, rather an object (?).

I also assumed that maybe the params object wasn't saving the sed edits, so I tried storing it like so:

def params = sh (script: ' echo $params | sed \'s/\\([[:alnum:]_][[:alnum:]_]*\\)/"\\1"/g\'', , returnStdout:true).trim()

Could someone please help, or move me in the right direction? Thanks.

suffocats
  • 11
  • 1
  • Instead of using `script` , can you use embedded shell script like this: sh '''echo $params ''' – mdabdullah Jul 16 '20 at 14:22
  • You are trying to store a shell output into a groovy variable. Refer this link: https://stackoverflow.com/questions/36547680/how-to-do-i-get-the-output-of-a-shell-command-executed-using-into-a-variable-fro – mdabdullah Jul 16 '20 at 14:25

1 Answers1

0

If you enclose the script in single quotes, the $params variable will not be expanded, so you're running the sed command over the string literal '$params' rather than the content. Other than that, you're on the right track.

It should be something like this (I haven't tested):

def params = sh (script: " echo $params | sed 's/\\([[:alnum:]_][[:alnum:]_]*\\)/\"\1\"/g'", , returnStdout:true).trim()
Mzzl
  • 3,926
  • 28
  • 39