为什么 zsh 隐藏 ac 程序的错误输出?

发布于 2025-01-10 21:57:18 字数 392 浏览 1 评论 0原文

我有一个用 C 编写的程序失败了。当我在 zsh 中运行它时,程序失败,但它的错误输出没有显示在命令行中(请注意,第二个➜是红色的,表示执行失败):

hpsc-hw2-USER on  master 
➜ ./sort -a 405500

hpsc-hw2-USER on  master 
➜ 

但是,当我在bash,出现错误:

[USER@COMPUTER hpsc-hw2-USER]$ ./sort -a 405500
Segmentation fault (core dumped)

知道为什么吗?如果有帮助的话,我正在运行带有太空飞船主题的 oh-my-zsh

I have a program written in C that fails. When I run it in zsh, the program fails, but it's error output is not displayed in command line (note that the second ➜ was red, indicating a failed execution):

hpsc-hw2-USER on  master 
➜ ./sort -a 405500

hpsc-hw2-USER on  master 
➜ 

However, when I run it in bash, the error shows up:

[USER@COMPUTER hpsc-hw2-USER]$ ./sort -a 405500
Segmentation fault (core dumped)

Any idea why? I'm running oh-my-zsh with the spaceship theme if that helps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

完美的未来在梦里 2025-01-17 21:57:18

但它的错误输出未显示在命令行中

这不是您的程序写入屏幕的内容。这是外壳在写它。所以外壳并没有“隐藏”它。

我自己没有使用 zsh,但由于红色箭头表示程序异常终止,我想您可以查看红色箭头的代码并创建自定义消息。这是一个有关错误代码的问题,可能会对您有所帮助:段错误的进程返回什么错误代码?

我记得我曾经制作过一个自定义的 bash 提示符,显示最后的退出代码。也许我用过这个,但我不确定: Bash Prompt with Last Exit Code

这里是有关如何自定义的文档太空飞船主题的提示: https://denysdovhan.com/spaceship-prompt/docs/Options.html#exit-code-exit_code

我假设您需要将 exit_code 添加到 .zshrc 文件中的 SPACESHIP_PROMPT_ORDER 部分。

知道为什么吗?

你可能得问开发商。一个同样有效的问题是:“为什么 bash 打印‘分段错误’?”这只是一个设计选择。 Bash 不会打印发生段错误的内存地址。这是为什么?

看来 oh-my-zsh 的开发者认为用红色箭头就足够了。

but it's error output is not displayed in command line

That's not something that your program writes to the screen. It's the shell that's writing it. So the shell is not 'hiding' it.

I'm not using zsh myself, but since a red arrow indicates that the program was abnormally terminated, I guess you can look at the code for the red arrow and create a custom message. Here is a question about the error codes that might help you: What error code does a process that segfaults return?

I remember that I once made a custom bash prompt that showed the last exit code. Maybe I used this, but I'm not sure: Bash Prompt with Last Exit Code

Here is the documentation for how to customize the prompt for spaceship theme: https://denysdovhan.com/spaceship-prompt/docs/Options.html#exit-code-exit_code

I assume that you need to add exit_code to the SPACESHIP_PROMPT_ORDER section in your .zshrc file.

Any idea why?

You probably have to ask the developers. An equally valid question is: "Why does bash print 'segmentation fault'?" It's just a design choice. Bash does not print the memory address where the segfault occurred. Why is that?

Seems like the developers of oh-my-zsh thought it was enough with a red arrow.

扛起拖把扫天下 2025-01-17 21:57:18

有什么想法如何让 zsh 输出错误消息吗?

最后一个命令的结果存储在 shell 变量 ? 中,因此您可以使用 print $?echo $? 等内容打印它。与大多数 shell 一样,zsh 具有令人难以置信的可配置性,因此您可以编写一个脚本,例如在提示符中包含最后一个命令的结果。这是一篇博客文章,其中作者将 zsh 配置为在正确的提示中显示非零结果: 显示 Zsh 中最后一个命令的退出代码

或者为什么它一开始就不这样做?

Shell 命令通常不会崩溃,如果遇到错误,它们通常会提供更有用的输出,而不仅仅是本身的返回代码。返回代码对于脚本最有用,因此您可以编写处理错误情况的脚本。

Any ideas how to make zsh output the error message?

The result of the last command is stored in the shell variable ?, so you can print it with something like print $? or echo $?. Like most shells, zsh is incredibly configurable, so you can write a script that e.g. includes the result of the last command in your prompt. Here's a blog post in which the author configures zsh to display non-zero results in the right prompt: Show Exit Code of Last Command in Zsh.

Or why it doesn't do it to begin with?

Shell commands don't normally crash, and if they encounter errors they typically provide more useful output than just the return code on their own. The return code is most useful for scripts, so that you can write a script that handle error conditions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文