为什么 zsh 隐藏 ac 程序的错误输出?
我有一个用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是您的程序写入屏幕的内容。这是外壳在写它。所以外壳并没有“隐藏”它。
我自己没有使用 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
的开发者认为用红色箭头就足够了。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 theSPACESHIP_PROMPT_ORDER
section in your.zshrc
file.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.最后一个命令的结果存储在 shell 变量
?
中,因此您可以使用print $?
或echo $?
等内容打印它。与大多数 shell 一样,zsh 具有令人难以置信的可配置性,因此您可以编写一个脚本,例如在提示符中包含最后一个命令的结果。这是一篇博客文章,其中作者将 zsh 配置为在正确的提示中显示非零结果: 显示 Zsh 中最后一个命令的退出代码。Shell 命令通常不会崩溃,如果遇到错误,它们通常会提供更有用的输出,而不仅仅是本身的返回代码。返回代码对于脚本最有用,因此您可以编写处理错误情况的脚本。
The result of the last command is stored in the shell variable
?
, so you can print it with something likeprint $?
orecho $?
. 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.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.