当命令有空格时,groovy流程执行问题

时间:2013-12-21 02:14:38

标签: groovy

为什么提交消息中的空格会导致p2失败?

def envp = null
def repoDir = File.createTempFile("test","txt").getParentFile()

Process p = "git init".execute(null, repoDir)
p.waitForProcessOutput(System.out, System.err)

/////////////

println "Commit1 start"
Process p1 = "git commit --allow-empty -m \"Commit1\"".execute(null, repoDir)
p1.waitForProcessOutput(System.out, System.err)
println "Commit1 done"


/////////////

println "Commit2 start"
Process p2 = "git commit --allow-empty -m \"Commit number 2\"".execute(null, repoDir)
p2.waitForProcessOutput(System.out, System.err)
println "Commit2 done"

1 个答案:

答案 0 :(得分:3)

以下代码解决了您的问题:

println "Commit2 start"
Process p2 = ["git", "commit", "--allow-empty", "-m \"Commit number 2\""].execute(null,  repoDir)
p2.waitForProcessOutput(System.out, System.err)
println "Commit2 done

此处记录了使用数组(而不是字符串)作为带引号的带参数的解决方法: http://groovy.codehaus.org/Executing+External+Processes+From+Groovy