-1

I can redirect the data stream by using < > and 2> for STDIN, STDOUT and STDERR.

Somehow I'm confused them with return value.

If return value is 1 or 2 how to understand?

If return value is -1 how to understand?

break can exit a loop but keep execute the following code, how about return? Once see return , it will terminate the main() with the return code, correct?

Lee William
  • 125
  • 1
  • 11
  • 4
    It depends on you. **Conventionally, returning 0 means that the program has terminated without any errors. All non-zero return values mean that the program ended with an error.** It is up to the programmer to designate `1` for a bad I/O operation, or `2` or a Segmentation Fault or whatever the choice is. The non zero return values are all up to the programmer designing the program. – shauryachats Apr 09 '15 at 05:34
  • " echo $?" will tell you that the last command was successful or not in unix. 0--> No error command executed successfully. non zero --> error – rabi shaw Apr 09 '15 at 05:43
  • In Unix, non-zero exit statuses are largely arbitrary. Most commands do not attempt to report different degrees of success with different exit statuses. (Those few that do are a confounded nuisance to work with, I might add.) However, POSIX mandates some exit statuses (126, 127, for example). Bash reports status 128+N as the status when a child dies from signal N. However, a process could exit with status 128+N without dying from a signal — the script running in the shell can't tell the difference, but doesn't often have to. – Jonathan Leffler Apr 09 '15 at 05:54
  • The exit code has nothing to do with the std streams. – Jim Balter Apr 09 '15 at 06:40
  • possible duplicate of [What should main() return in C and C++?](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – Lee William May 15 '15 at 10:23

3 Answers3

2

To answer this, let's refer to the C99 standard. If you read on 7.20.4.3 The exit function, you'll find the following statement:

5 Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

What should be emphasized here is that the C standard doesn't specify any specific codes for exit status. While in the snippet above you can see zero mentioned, it's purely part of the exit() API. Rephrasing the remainder of the sentence, that zero can be mapped onto any other implementation-defined successful termination. Similarly, there's no guarantee that the definitions of EXIT_SUCCESS and EXIT_FAILURE won't be mapped onto other codes. In particular, it may not even end up as an integer.

Furthermore, if you look at 7.20.4.6 The system function, you'll notice:

Returns 3 If the argument is a null pointer, the system function returns nonzero only if a command processor is available. If the argument is not a null pointer, and the system function does return, it returns an implementation-defined value.

Which confirms that exit codes are purely implementation-defined, and you can't rely on any particular behavior.


Now, POSIX extends this a bit. If you read on the stdlib.h header, you'd find:

EXIT_FAILURE

Unsuccessful termination for exit(); evaluates to a non-zero value.

EXIT_SUCCESS

Successful termination for exit(); evaluates to 0.

This already restricts the implementations to use 0 for a successful exit. However, it doesn't really define which of the non-zero values is used for failure.

If you read on exit() function, you'd find:

The value of status may be 0, EXIT_SUCCESS, EXIT_FAILURE, or any other value, though only the least significant 8 bits (that is, status & 0377) shall be available to a waiting parent process.

Which also extends the C definition by guaranteeing that the exit status in range 0…255 will be made available to other processes.

So, POSIX defines zero as successful exit, EXIT_FAILURE (some non-zero value) as unsuccessful exit and leaves the remaining values for program use.

However, if you are parsing termination codes from other processes, you should take a look at wait() function as well. It defines additional macros used to process the return status for non-normal termination statuses, such as termination by signal.


If we go even further, we can find that FreeBSD introduces sysexits.h header to define exit codes even further.

The successful exit is always indicated by a status of 0, or EX_OK. Error numbers begin at EX__BASE to reduce the possibility of clashing with oth­ er exit statuses that random programs may already return. The meaning of the codes is approximately as follows:

EX_USAGE (64) The command was used incorrectly, e.g., with the wrong number of arguments, a bad flag, a bad syntax in a parameter, or whatever. […]

Which means that applications written for FreeBSD may use well-defined exit codes 64+.


To summarize shortly:

  1. The C standard defines only the API for returning successful and unsuccessful exit. It doesn't define how to use, pass or obtain that result.
  2. POSIX strictens that to using zero for success, and guarantees that in case of normal process termination, the 8 least significant bits of exit code will be made available to other processes (e.g. via wait()).
  3. FreeBSD reserves exit codes 64+ to create well-defined error condition exit codes.

That's how far the standards go. Now, in the reality most programs use zero to indicate plain success, and non-zero codes to indicate various other statuses. Programs using exit codes to pass specific information often describe them in manpages.

For example, if you look at GNU cmp:

An exit status of 0 means no differences were found, 1 means some differences were found, and 2 means trouble.

Michał Górny
  • 18,713
  • 5
  • 53
  • 76
1

as mentioned in comments if any command execution returns 0 then it indicates successful execution, whereas any non-zero value means error while executing that particular command. after execution of command the return value can be seen by echo $? in unix. most of the time the non zero return value indicating error, i.e. the return value is predefined for known errors. these are also called as exit codes.

There has been an attempt to systematize exit status numbers (see /usr/include/sysexits.h), but this is intended for C and C++ programmers. A similar standard for scripting might be appropriate.

also

For the shell’s purposes, a command which exits with a zero exit status has succeeded. A non-zero exit status indicates failure. This seemingly counter-intuitive scheme is used so there is one well-defined way to indicate success and a variety of ways to indicate various failure modes. When a command terminates on a fatal signal whose number is N, Bash uses the value 128+N as the exit status.

If a command is not found, the child process created to execute it returns a status of 127. If a command is found but is not executable, the return status is 126.

If a command fails because of an error during expansion or redirection, the exit status is greater than zero.

All of the Bash builtins return an exit status of zero if they succeed and a non-zero status on failure, so they may be used by the conditional and list constructs. All builtins return an exit status of 2 to indicate incorrect usage.

Himanshu Sourav
  • 700
  • 1
  • 10
  • 35
1

MSDOS exit code is only 1 byte, and there may a prior precedent for this. Windows continued this standard, limiting exit codes to the range 0 to 255. The MSDOS / Windows command interpreter stores the exit code in a command interpreter variable named ERRORLEVEL which can be used in batch files. There's also an environment variable named %ERRORLEVEL% that defaults to ERRORLEVEL, but the environment variable can be set to something other than ERRORLEVEL. There may be cases where the exit code is used for something like switch case statement in C, as opposed to being an error indicator. I'm not sure how exit codes can be used *nix operating systems scripts.

rcgldr
  • 27,407
  • 3
  • 36
  • 61