Exit code from Exit (-1) or return ( -1) shell Why is the 255_linux shell

Source: Internet
Author: User

Now I've written a Hello World program to take a look at it:

Filename:main.c
 #include <stdio.h>
 int main (void)
 {
   printf ("Hello wolrd!\n");
   return (-);  
 }

Compile execution: gcc main.c &&/a.out

Now let's take a look at the return value of the previous execution in the current shell, is "-1"?

inuyasha@inuyasha-aspire-4741:~/Desktop $ gcc main.c &&/a.out

Hello world!

inuyasha@inuyasha-aspire-4741:~/Desktop $ echo $?

255

Ah, the result why "255"? Call a program, program exit-1, get results not "-1"?

The following references from: http://www.laruence.com/2012/02/01/2503.html

The problem is simply that the return in exit or main function can only use the value between 0~255. -The unsigned value of 1 is 255.

What about the complicated point?

We know that in a shell, a command, a program, is executed by a fork (then exec), and the exit code of the program is collected by the shell (the parent process) and then reported to us.

pid_twait (int *statloc);

And for wait, historically, he will return a 16bit interge via Statloc (now also 32-bit, but compatible with existing designs). In this 16bits interge, the high 8 bits are the value of the program exit (exit, or return), while the low eight digits indicate the signal that caused the program to exit (one of them indicates whether a core file is produced), and if the program is normal exit, then the lower eight bits are 0[1].

So, if we return 1, and because we are exiting normally, the child process that the shell collects through wait is:

11111111 00000000

And the high eight-bit as a unsigned, is 255.

In addition, to add, in the Linux built-in shell commands, many will adhere to an exit status code agreement, the specific value of the corresponding meaning [2]:

number
Exit Codemeaning Example Comments
1 Catchall for general errors Let "var1 = 1/0″ Miscellaneous errors, such as "divide by zero" and other impermissible operations
2 Misuse of Shell Builtins (according to Bash documentation) Empty_function () {} Seldom seen, usually defaults to exit code 1
126 Command invoked cannot execute Permission problem or command is not a executable
127 "Command not Found" Illegal_command Possible problem with $PATH or a typo
128 Invalid argument to exit Exit 3.14159 Exit takes only-integer args in the range 0–255 (I/footnote)
128+n Fatal error Signal "n" kill-9 $PPID of script $? returns 137 (128 + 9)
130 Script terminated by Control-c CONTROL-C is fatal error signal 2, (130 = 128 + 2, and so on above)
255* Exit Status out of range Exit-1 Exit takes only-integer args in the range 0–255

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.