“find: missing argument to `-exec'” running in a Java process builder

耗尽温柔 提交于 2019-12-01 20:53:52

The problem for the error with \; is that you are mixing shell escaping/quoting with the plain passing of params of the exec functions.

Drop the \ before the ; and it works. ; needs \ only in the shell, since it is used to separate commands there. Same goes for quoting {} - when passing params to the exec*-style functions, no shell-style quoting/escaping is needed, since no shell interprets it (unless of course you run sh -c):

def command = 'find . -name build.xml -exec echo {} ;' // XXX
new ProcessBuilder()
    .directory(new File("/tmp"))
    .inheritIO()
    .command(command.split(" ")) // everything is just a list of strings
    .start()

And this is basically the same thing in groovy:

("/tmp" as File).eachFileRecurse{
    if (it.name=="build.xml") {
        println it
    }
}

Can't you replace all of the above with:

String output = ['bash', '-c', 'find . -name "*.xml" -exec echo "{}" \\;']
    .execute(null, new File('/tmp')).text
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!