当命令有空格时groovy进程执行出现问题

当命令有空格时groovy进程执行出现问题,groovy,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

为什么提交消息中的空格会导致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"

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

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
此处记录了使用数组而不是字符串作为带空格的引号参数问题的解决方法:
以下代码解决了您的问题:

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
此处记录了使用数组而不是字符串作为带空格的引号参数问题的解决方法:

您是否尝试过git commit-allow empty-m'commit number 2'?带单引号。是的。我也试过了。没用。我在Mac Mavericks上你试过git commit-allow empty-m‘commit number 2’吗?带单引号。是的。我也试过了。没用。我在Mac小牛队