DEV Community

taijidude
taijidude

Posted on

2

There is always a carriage return after a bash output...

This week i ran into the following problem:

In a jenkins pipeline file i ran the shell command pwd and stored the result into a groovy variable. Did this to get a specific path which I needed to execute a groovy file from a shell statement. It looked something like this:


   script{ p1 = sh(returnStdout: true, script: "pwd") }
   sh "groovy deploy.groovy -path1 ${p1} -nextParameter"

Enter fullscreen mode Exit fullscreen mode

Jenkins made two shell statements out of this, and tried to execute them separate from each other.

groovy deploy.groovy -path1 /d/temp/
-nextParameter
Enter fullscreen mode Exit fullscreen mode

The solution: I forgot that after nearly every output(? need to look into this) from the bash you get a carriage return and newline. That is why Jenkins did interpret this as two separate statements. Fixed it by trimming the output of the sh command. This removes not only whitespace but also non printable characters.

   script{ p1 = sh(returnStdout: true, script: "pwd").trim() }
   sh "groovy deploy.groovy -path1 ${p1} -nextParameter"
Enter fullscreen mode Exit fullscreen mode

This works!

groovy deploy.groovy -path1 /d/temp/ -nextParameter
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (2)

Collapse
 
gabrielgomesmeira profile image
Gabriel Gomes Meira

Thanks! Helped me.

Collapse
 
taijidude profile image
taijidude

you are welcome :-)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay