Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Is it acceptable to return a non-zero exit code if the program in question ran properly? For example, say I have a simple program that (only) does the following:

Program takes N arguments. It returns an exit code of min(N, 255). Note that any N is valid for the program.

A more realistic program might return different codes for successfully ran programs that signify different things. Should these programs instead write this information to a stream instead, such as to stdout?

share|improve this question
add comment

6 Answers

up vote 14 down vote accepted

It depends on the environment, but I'd say it's poor style.

Unix-like systems have a strong convention that an exit status of 0 denotes success, and any non-zero exit status denotes failure. Some, but not all, programs distinguish between different kinds of failures with different non-zero exit codes; for example grep typically returns 0 if the pattern was found, 1 if it wasn't, and 2 (or more) if there was an error such as a missing file.

This convention is pretty much hard wired into Unix shells. For example, in sh, bash, and other Bourne-like shells, the if statement treats a 0 exit status as success/true, and a non-zero exit status as failure/false:

if your-command
then
    echo ok
else
    echo FAILURE
fi

I believe the conventions under MS Windows are similar.

Now there's certainly nothing stopping you from writing your own program that uses unconventional exit codes, especially if nothing else is going to interact with it, but be aware that you're violating a well established convention, and it could come back and bite you later.

The usual way for a program to return this kind of information is to print it to stdout:

status = $(your-command)
echo Result is $status
share|improve this answer
1  
+1 for explaining the convention, that approach would break most of my shell scripts were I put a set -e somewhere. –  Benjamin Bannier Jun 22 '12 at 20:56
add comment

depends on what your environment expects:

my favorite for wierdness, from wikipedia:

In OpenVMS success is indicated by odd values and failure by even values. The value is a 32 bit integer with sub-fields: control bits, facility number, message number and severity. Severity values are divided between success (Success, Informational) and failure (Warning, Error, Fatal)

http://en.wikipedia.org/wiki/Exit_status

share|improve this answer
add comment

not returning 0 for a successful run is a bad idea, because it can cause confusion for those running your program. What if some ran your program 100+ times with different input and wanted to know how many failed or completed successfully, having a single success value makes this much easier to spot differences than having many different values.

If you have a program that has multiple successful return paths that can all signify different things, I would say that is a sign your program is poorly designed.

share|improve this answer
add comment

I know of a case I think is acceptable. I know of a testing framework that exits with the total number of test failures. So, for example, if the test runner completes with no failed tests it exits with zero. If one test failed, even though the test runner itself ran propely, it exits with one 1. If two fail it returns 2, etc. This goes up to 250, which means "250 or more test failures".

It uses exit codes > 250 to signify abnormal exits.

While this violates convention, it works well in practice.

share|improve this answer
1  
I don't think this does violate convention - testing is successful if there are no errors; any non-zero result code indicates failed tests. –  Bevan Jun 23 '12 at 2:57
    
That's "exiting with an error status to indicate more than 0 errors" and while it may violate the exact common semantics of the exit status, it certainly doesn't violate the larger "0 is OK, anything larger than 0 is an error" convention. –  Vatine Jun 24 '12 at 8:34
add comment

It really depends what you are trying to convey with the code. DB2, for example returns 100 if there is no data found, various other positive values for warnings, and negative values for errors. Oracle does something similar.

So, if there are different success states, it may be worth using varying return values.

share|improve this answer
add comment

I think there is a precedent if the exit code is returning meaningful, relevant information to the caller and the definition of success isn't really binary. The precedent I'm thinking of is robocopy which returns a slew of different things depending on what happened.

I'll add that we always end up having some debugging because of this -- most utility assume exit code 0 == success so get freaked out when robocopy returns 1 because it copied stuff not zero because it didn't copy stuff but didn't get an error either.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.