Go的exec.Command()函数返回退出状态码64。

huangapple go评论59阅读模式
英文:

Go exec.Command() returns exit status 64

问题

我正在翻译以下内容:

我正在研究在Go语言中使用exec.Command()函数。这个函数很简单,使用像"ls"、"cat"等基本终端命令没有问题。

但是,当我想要在macOS中使用"log"命令的输出时,它总是返回一个错误。

以下是它的简单用法:

func main() {
    out, err := exec.Command("log", "help").Output()
    if err != nil {
        log.Printf("error: %v", err)
    }

    fmt.Println(string(out))
}

以下是错误信息:

2022/11/19 20:03:37 error: exit status 64

我期望能够看到在macOS中执行"log help"命令的输出。

但是,我的程序返回了一个带有"exit status 64"的错误。

请告诉我是否有什么我忽略的地方。

英文:

I was figuring out

> exec.Command()

In Go. It is straightforward and has no problem using primary terminal commands like "ls", "cat", etc...

But, when I wanted to use the output of the "log" command in macOS. It always returns an error.

Here is the simple usage of it:

func main() {
    out, err := exec.Command("log", "help").Output()
    if err != nil {
        log.Printf("error: %v", err)
    }

    fmt.Println(string(out))
}

And here is the error:

2022/11/19 20:03:37 error: exit status 64

I expected to see outputs of log help in macOS.

But, my program returns an error with exit status 64

Please let me know if there is anything I missed.

答案1

得分: 0

你的代码没有问题,如果你在终端中执行完全相同的命令,并查看退出状态,它将是64

➜  ~ log help
usage:
    log <command>

global options:
    -?, --help
    -q, --quiet
    -v, --verbose

commands:
    collect         gather system logs into a log archive
    config          view/change logging system settings
    erase           delete system logging data
    show            view/search system logs
    stream          watch live system logs
    stats           show system logging statistics

further help:
    log help <command>
    log help predicates

➜  ~ echo $?
64

如果你想知道为什么返回64,你可以询问sysexits它的含义:

➜  ~ man sysexits | grep -A 3 '64'
     EX_USAGE (64)         The command was used incorrectly, e.g., with the
                           wrong number of arguments, a bad flag, a bad syntax
                           in a parameter, or whatever.
英文:

There's nothing wrong with your code, if you try the exact same command you're executing in terminal, and see the exit status, it will be 64:

➜  ~ log help
usage:
    log &lt;command&gt;

global options:
    -?, --help
    -q, --quiet
    -v, --verbose

commands:
    collect         gather system logs into a log archive
    config          view/change logging system settings
    erase           delete system logging data
    show            view/search system logs
    stream          watch live system logs
    stats           show system logging statistics

further help:
    log help &lt;command&gt;
    log help predicates

➜  ~ echo $?
64

And in case you're wondering why is it returning 64, you can ask sysexits for what it means:

➜  ~ man sysexits | grep -A 3 &#39;64&#39;
     EX_USAGE (64)         The command was used incorrectly, e.g., with the
                           wrong number of arguments, a bad flag, a bad syntax
                           in a parameter, or whatever.

huangapple
  • 本文由 发表于 2022年11月20日 03:20:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/74502972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定