Output of shell command into a variable

26 views
Skip to first unread message

Sam K

unread,
Oct 21, 2016, 2:06:06 PM10/21/16
to Jenkins Users
Currently, I'm doing this.  

sh '<some-command>  > /tmp/aa'
def VAR1 = readFile('/tmp/aa')

This mostly works.  But oftentimes the variable has a newline and so it messes up its usage. 

Is there a better way to store the output in a variable without having to deal with the newline? 

thanks

Mark Waite

unread,
Oct 21, 2016, 8:20:16 PM10/21/16
to Jenkins Users
If you're inside a pipeline script and calling sh (or bat), then you can use a slightly different syntax and it will assign the output of the shell command as the return value.


Mark Waite

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/f12a1391-bf74-41f9-9c6e-9eab354b7ea6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sam K

unread,
Oct 21, 2016, 10:36:18 PM10/21/16
to Jenkins Users

Your answer works like a charm, but will still give me a problem with the newline in the end.  I added the trim() from the stackoverflow answer and now the output is what I want.  

Thanks again!!
Sam
Reply all
Reply to author
Forward
0 new messages