Jeff Storey
Jeff Storey

Reputation: 57192

groovy syntax for executing unix command with process output

I'm trying to execute a shell command in groovy. The command is diff <(ls dir1) <(ls dir2). From the shell, that works fine, but when I do "diff <(ls dir1) <(ls dir2)".execute() in groovy, I get a shell error saying diff: extra operand. I can't seem to figure out the correct syntax for this in groovy. Can someone point out what I'm doing wrong?

Upvotes: 1

Views: 1410

Answers (1)

cfrick
cfrick

Reputation: 37008

If you can live with calling this via bash like you do on command line, then the syntax to call by shell is:

def p = ["/bin/bash", "-c", "diff <(ls dir1) <(ls dir2)"].execute()
p.waitFor()
println p.text

The reason you can not simply run above code: execute() does just a simple process execution. So you can only run commands and pass param. So e.g. 'diff file1 file1'.execute() will work. But the <(...) is bash-speak for "create me a named pipe". But you can pass to any(?) shell a "command" with -c param to execute it, so you can utilize the power of the shell. Calling it as an array of strings in my example saves you the hastle with quoting/escaping everything properly.

Upvotes: 2

Related Questions