gpt4 book ai didi

bash - 如何使用 Go 在命令行上执行 diff?

转载 作者:IT王子 更新时间:2023-10-29 01:33:39 25 4
gpt4 key购买 nike

我在使用 Ubuntu 14.04 和在命令行上执行 diff 时遇到问题。看下面的 Go 代码:

package main

import "fmt"
import "log"
import "os/exec"

func main() {
output, err := exec.Command("diff", "-u", "/tmp/revision-1", "/tmp/revision-4").Output()
if err != nil {
log.Fatalln(err)
}

fmt.Println(string(output))
}

如果我使用 go run test.go 执行此操作,我会收到以下错误:

2015/03/18 14:39:25 exit status 1
exit status 1

所以 diff 出了点问题,它返回 1 作为退出代码。只有 diff 命令似乎会引发错误。如果我使用 catwc 命令,代码运行正常。

知道为什么 diff 在这里不起作用但其他命令可以吗?

最佳答案

当您使用 exec 运行程序时,如果退出代码不是 0,则会出现错误。来自文档:

The returned error is nil if the command runs, has no problems copying stdin, stdout, and stderr, and exits with a zero exit status.

If the command fails to run or doesn't complete successfully, the error is of type *ExitError. Other error types may be returned for I/O problems.

所以这里发生的是 diff 在文件不同时返回错误,但您将其视为运行时错误。只需更改您的代码以反射(reflect)它不是。通过检查错误是可能的。

例如像这样:

    output, err := exec.Command("diff", "-u", "/tmp/revision-1", "/tmp/revision-4").CombinedOutput()
if err != nil {

switch err.(type) {
case *exec.ExitError:
// this is just an exit code error, no worries
// do nothing

default: //couldnt run diff
log.Fatal(err)
}
}

此外,我已将其更改为 CombinedOutput,因此如果发生任何 diff 特定错误,您也会看到 stderr。

请注意,即使其中一个文件不存在,您也会收到“有效”错误。因此,您可以通过执行以下操作来检查 ExitError 的退出代码:

    switch e := err.(type) {
case *exec.ExitError:

// we can check the actual error code. This is not platform portable
if status, ok := e.Sys().(syscall.WaitStatus); ok {
// exit code 1 means theres a difference and is not an error
if status.ExitStatus() != 1 {
log.Fatal(err)
}
}

关于bash - 如何使用 Go 在命令行上执行 diff?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29125248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com