修补程序命令的exec.command [英] exec.command for patch command

查看:153
本文介绍了修补程序命令的exec.command的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下命令修补文件

I am trying to patch a file using the below command

patch -p0 < <file_path>

我的runCommand语法如下:

My runCommand syntax is as below:

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
}

现在,我正在通过以下修补程序命令:

Now I am passing my patch command as below:

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

但是我看到以下错误:

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

你能让我知道我在这里想念什么吗?

Can you please let me know what I am missing here?

推荐答案

您从

与使用C和其他语言进行的系统"库调用不同, os/exec软件包有意不调用系统外壳程序,并且 不会扩展任何glob模式或处理其他扩展, 管道,或通常由Shell完成的重定向.包装 行为更像C的"exec"函数系列.扩大球 模式,要么直接调用外壳,要么小心逃逸 危险的输入,或使用路径/文件路径包的Glob函数.到 扩展环境变量,请使用软件包os的ExpandEnv.

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.

shell负责处理<操作.您可以使用文件作为输入自己设置stdin,也可以使用外壳程序.要使用外壳,请尝试以下操作:

The shell is responsible for handling < operations. You can set stdin yourself with the file as input or you can use the shell. To use the shell, try something like:

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

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

Note that this won't work on Windows. Reading the file and writing to stdin yourself is a more easily portable solution.

这篇关于修补程序命令的exec.command的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆