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

taijidude - Dec 5 '20 - - Dev Community

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
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player