命令执行,但对于不存在的命令,代码没有返回错误。

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

Command executes but code doesn't return error for non existing commands

问题

在运行fzf命令时,你发现代码没有返回错误,而且成功运行。然后,你将命令更改为一个不存在的命令,但代码仍然没有返回"couldn't call command: command not found",而是直接退出。你不知道可能出了什么问题。

以下是翻译好的代码:

reader := strings.NewReader(listOutput.String())

r, w, _ := os.Pipe()
os.Stdout = w

cmd := exec.Command("fzf", "--multi")
cmd.Stdin = reader
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
    fmt.Println("无法调用fzf:%v", err)
}

w.Close()

然后,你将命令更改为一个不存在的命令,但代码仍然没有返回"couldn't call command: command not found",而是直接退出。

reader := strings.NewReader(listOutput.String())

r, w, _ := os.Pipe()
os.Stdout = w

cmd := exec.Command("idontexist")
cmd.Stdin = reader
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
    fmt.Println("无法调用命令:%v", err)
}

w.Close()

你不知道可能出了什么问题。

英文:

Code in function to run a fzf against an input, while debugging i discovered my code doesn't return errors, this code runs successfully:

	reader := strings.NewReader(listOutput.String())

	r, w, _ := os.Pipe()
	os.Stdout = w

	cmd := exec.Command("fzf", "--multi")
	cmd.Stdin = reader
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		fmt.Println("Couldn't call fzf: %v", err)
	}

	w.Close()

So i changed the command to something that doesn't exist, but the code still doesn't return "couldn't call command: command not found", just exits.

	reader := strings.NewReader(listOutput.String())

	r, w, _ := os.Pipe()
	os.Stdout = w

	cmd := exec.Command("idontexist")
	cmd.Stdin = reader
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		fmt.Println("Couldn't call command: %v", err)
	}

	w.Close()

I don't have an idea what could be wrong.

答案1

得分: 4

cmd.Run()返回一个错误,并且你的if代码块得到了正确执行,但是由于你改变了标准输出os.Stdout = w,所以你在控制台/终端上看不到结果。

fmt包将内容写入标准输出。

如果你使用log包,你将会看到错误信息,因为log包将内容写入标准错误(你没有改变它):

log.Printf("无法调用命令:%v", err)

这将输出类似以下内容(注意默认的日志格式包括时间戳):

2022/12/07 13:46:19 无法调用命令:exec: "idontexist": 可执行文件在$PATH中未找到

或者不要改变标准输出。

另外请注意,log.Println()fmt.Println()不需要格式化字符串。当你指定格式化字符串和参数时,请使用log.Printf()fmt.Printf()

英文:

cmd.Run() does return an error, and your if block gets properly executed, but since you change the standard output os.Stdout = w you just don't see the result on your console / terminal.

The fmt package writes to the standard output.

If you use the log package, you will see the error as the log package writes to the standard error (which you didn't change):

log.Printf("Couldn't call command: %v", err)

This will output something like (note the default log format includes the timestamp too):

2022/12/07 13:46:19 Couldn't call command: exec: "idontexist": executable file not found in $PATH

Or don't change the standard output.

Also do note that log.Println() and fmt.Println() do not require a format string. Do use log.Printf() and fmt.Printf() when you specify a format string and arguments.

huangapple
  • 本文由 发表于 2022年12月7日 20:35:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/74716790.html
匿名

发表评论

匿名网友

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

确定