2017-12-02 4 views
-1

私は私のrunCommand関数の構文は以下の通りですexec.command

patch -p0 < <file_path> 

以下のコマンドを使用してファイルにパッチを適用しようとしています:

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 
} 

今、私は以下のように私のpatchコマンドを渡しています:

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を自分で設定するか、シェルを使用することができます。シェルを使用するには、次のように試してみてください。

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

これはWindowsでは機能しません。ファイルを読み込んでstdinに書き込むことは、より簡単に移植可能なソリューションです。

関連する問題