2017-12-02 226 views
-1

我尝试使用下面的命令exec.command补丁命令

patch -p0 < <file_path> 

我runCommand语法如下修补文件:

func runCommand(cmd string, args ...string) error { 
    ecmd := exec.Command(cmd, args...) 
    ecmd.Stdout = os.Stdout 
    ecmd.Stderr = os.Stderr 
    ecmd.Stdin = os.Stdin 
    err := ecmd.Run() 
    return err 
} 

现在,我通过我的补丁命令,如下:

cmd = "patch" 
args := []string{"-p0", "<", "/tmp/file"} 
err = runCommand(cmd, args...) 

但我看到下面的错误:

补丁:****找不到文件“<”:没有这样的文件或目录

能否请你让我知道我在这里失踪?

回答

3

你错过了从documentation这一段:

Unlike the "system" library call from C and other languages, the os/exec package intentionally does not invoke the system shell and does not expand any glob patterns or handle other expansions, pipelines, or redirections typically done by shells. The package behaves more like C's "exec" family of functions. To expand glob patterns, either call the shell directly, taking care to escape any dangerous input, or use the path/filepath package's Glob function. To expand environment variables, use package os's ExpandEnv.

外壳是负责处理<操作。您可以使用文件作为输入来设置stdin,或者您可以使用shell。要使用外壳,请尝试如下所示:

runCommand("/bin/sh", "patch -p0 < /tmp/file") 

请注意,这不适用于Windows。阅读文件并自己写入stdin是一个更容易移植的解决方案。