7

I have an output string that I want to run a 'tr' and 'jq' command against. Piping makes sense like so, IP= sh(script: "echo $spawnServer | jq .[0] | tr -d '\"'", returnStdout: true) Unfortunately the jenkins pipeline hates pipes, so what I get is

+ tr -d '"'
+ jq '.[0]'
+ echo '[' 172.31.79.253, 'i-0d65b431f18a385d0]'
parse error: Invalid numeric literal at line 1, column 16

Any tips would be great! the only thing I found so far was someone using eval, but that didn't work for me. Any tips would be great!

pcort
  • 419
  • 1
  • 6
  • 19
  • I think you are missing quotes around your `jq` filter - `jq '.[0]'`. Quotes are a whole other story, and you will probably hot some issues with your `tr` - see https://gist.github.com/Faheetah/e11bd0315c34ed32e681616e41279ef4 – mkobit Nov 09 '17 at 04:43

1 Answers1

3

Instead of struggling with quotes and escapes, you could use def, as in:

def command = $/"echo ${spawnServer} | jq .[0] | tr -d '\"'"/$

res = sh(returnStdout: true, script: command).trim()
sh("echo ${res}")
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Minor improvement, stuff appears to be all on one line now, and getting a 'command not found' error. So I'll continue to poke at that. Also from a code stand point, are you missing a leading quote in the command expression? Should it be this: `$/"echo ${spawnServer} | jq .[0] | tr -d '\"'"/$` ? – pcort Nov 09 '17 at 12:54
  • @pcort yes on the missing double-quote (fixed). I am interested in the result of your poking. – VonC Nov 09 '17 at 13:34
  • I struggled with this quite a lot. The `not found` error is caused by the double quotes coming after `$/` and coming before `/$`. The command should be like `$/echo "something..."/$`. No double quotes. – Turgut Sarıçam Jul 29 '19 at 21:57