1

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I'll try that workaround. But I'm really trying to figure out WHY it doesn't work how I tried it.
@JeffStorey i have added some explanation. Basically it is like | or other "shellisms". The regular unix execute can not understand this -- only cmd param1 ... paramN

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.