Dan*_*izu 4 groovy find processbuilder jenkins
我试图在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();
Run Code Online (Sandbox Code Playgroud)
Web UI将显示println的结果:
find . -name build.xml -exec echo "{}" \;
Run Code Online (Sandbox Code Playgroud)
jenkins日志(/var/log/jenkins/jenkins.log)记录以下错误:
find: missing argument to `-exec'
Run Code Online (Sandbox Code Playgroud)
但是,如果我find . -name build.xml -exec echo "{}" \;通过shell 运行web UI()中输出的相同命令,则不会出现此类错误.
另外,如果我替换\;witih +,命令有效!
因此,使用processBuilder并且\\;作为命令行参数传递的东西是可疑的
错误的问题\;在于你将shell转义/引用与exec函数的params的普通传递混合在一起.
放下\之前;它就可以了. 只;需要在shell中,因为它用于在那里分离命令.同样适用于引用- 当将params传递给-style函数时,不需要shell样式的引用/转义,因为没有shell解释它(当然除非你运行):\ {}exec*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()
Run Code Online (Sandbox Code Playgroud)
这在groovy中基本上是相同的:
("/tmp" as File).eachFileRecurse{
if (it.name=="build.xml") {
println it
}
}
Run Code Online (Sandbox Code Playgroud)