groovy - "find: missing argument to ` -exec '"在 Java 进程构建器中运行

标签 groovy jenkins find processbuilder

我正在尝试在 Jenkins ( https://jenkins-ci.org ) 脚本控制台中运行 find 命令,该控制台允许从 Web 界面运行 groovy 脚本。

我的代码是:

ProcessBuilder pb = new ProcessBuilder();
pb.directory(new File("/var/lib/jenkins/jobs/myJob");
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
command = 'find . -name build.xml -exec echo \"{}\" \\;'
println(command)
pb.command(command.split(" "));
pb.start().waitFor();

Web UI 将显示 println 的结果:

find . -name build.xml -exec echo "{}" \;

虽然 jenkins 日志 (/var/log/jenkins/jenkins.log) 记录以下错误:

find: missing argument to `-exec'

但是,如果我通过 shell 运行在 Web UI 中输出的相同命令 (find . -name build.xml -exec echo "{}"\;),则不会出现此类错误。

此外,如果我用 + 替换 \;,该命令就可以工作!

所以 processBuilder 和作为命令行参数传递的 \\; 有点可疑

最佳答案

\; 错误的问题在于您将 shell 转义/引用与 exec 函数的参数的普通传递混合在一起。

\ 放在 ; 之前即可。 ; 仅在 shell 中需要 \,因为它用于分隔命令。引用 {} 也是如此 - 将参数传递给 exec* 风格的函数时,不需要 shell 风格的引用/转义,因为没有 shell 解释它(除非当然你运行 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()

这与 groovy 中的基本相同:

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

关于groovy - "find: missing argument to ` -exec '"在 Java 进程构建器中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33395955/

相关文章:

groovy - Gradle - 不允许设置 maxHeapSize?

java - groovy 中的 Join() 不等待线程完成

Android Gradle 从 strings.xml 读取应用程序名称

c++ - 在 C++ 中,如何提取文本文件的 ExtractFilePath

VBA使用通配符在行中查找值

maven - Grails 无法启动,卡在 postProcessBeanFactory

continuous-integration - Jenkins/Hudson - 访问当前版本号?

linux - Jenkins 无法完成执行 "while true"循环的构建脚本

arrays - 在 Groovy 中检索数组数组中元素的索引

excel - 使用 Excel 查找功能时避免触发 'Select' 触发器