groovy syntax for executing unix command with process output

六月ゝ 毕业季﹏ 提交于 2020-07-22 21:36:01

问题


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:


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.



来源:https://stackoverflow.com/questions/27277377/groovy-syntax-for-executing-unix-command-with-process-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!