Home > Net >  zsh doesn't show signal/error name for a failed C executable when run from a script
zsh doesn't show signal/error name for a failed C executable when run from a script

Time:03-21

I just found that my zsh don't alert me when there's a runtime error. Can anyone help me?

int main(void) {
    int a = 1;
    int b = 0;
    int c = a/b;
    return 0;
}

I compiled it and wrote a script to run it,

./a.out

I then entered zsh a.sh. It finished without any error.

But when I run it with the command line ./a.out it showed: zsh: floating point exception ./a.out

I'm using MacOS Monterey 12.2.1 and my zsh is zsh 5.8 (x86_64-apple-darwin21.0).

Really thanks!

I now understand that interactive and non-interactive shells have many different behaviors but I still don't know if there's a way zsh can tell me the error. (especially when the script is in .zshrc) The error message is kind of important.

CodePudding user response:

The error message you see is specific to the interactive shell, which detects that your program exit with a floating-point exception and reports it. It's not clear how to make a non-interactive shell report the error similarly, but it would be better to do such error-checking yourself. For example,

./a.out
case $? in
  0) print "Success" ;;
  *) print "Error: $?" ;;
esac
  • Related