João Amaro
João Amaro

Reputation: 496

Shell script output to array in groovy

I need to output the result of this shell script "git diff --name-only commit2 commit1" to an array in groovy. How can I do it?

Already tried to create a variable

def diff = sh(script: "git diff --name-only commit2 commit1", returnStdout: true)

and then work it with Pattern and Matcher, but probably due to its formatting it is always returning an empty array.

The output of the shell script is something like:

directory/file1.java
directory/file2.java

Found a way to do it with bash but can't figure out how to adapt it to groovy (mapfile -t my_array < <( my_command ))

Upvotes: 1

Views: 4259

Answers (1)

Szymon Stepniak
Szymon Stepniak

Reputation: 42174

If you want to get the output as a list, where each line is a separate entry in that list, you could call split('\n') on the output of the sh step:

def diff = sh(script: "git diff --name-only commit2 commit1", returnStdout: true).trim().split('\n')

Upvotes: 5

Related Questions