1

If a process runs and exits it returns a non-zero status (personnaly I prefer the term return code); or even a zero status I know that the value is there (mostly because I've done C++ programming and I know you can at the end of your main() method return a value...but also because if I run something like:

fuser -s ./myfile.txt && echo "a process is accessing your file"

I know that the echo only prints if the status value returned from an fuser process is 0, if it is not, echo is skipped all together because:

"fuser returns a non-zero return-code if none of the specified files is accessed or in case of a fatal error."

Now how do I display this return code to know it's value? Because if you're running fuser with -silent, such as in a script, it would seem important to know the value of the return-code/status so I can tell the difference between a file that isn't being accessed and an actual fatal error.

Also, is there a common place in the man pages where return codes / status for a process are documented so I can see what the value might mean? Or is there a document that has standards for what return codes should be?

leeand00
  • 4,615

1 Answers1

6

You can access the return code of the last command executed with the special parameter $?. There is no documented standard (at least none that are widely adopted) for return codes other than "0" being success and non-zero being a failure. You will have to check the manpage of the specific command that you are running.

jordanm
  • 42,678
  • 1
    http://unix.stackexchange.com/questions/255976 has more details. – Stephen Kitt Jan 28 '16 at 17:15
  • 1
    No documented standard, besides that of http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/sysexits.3 (as for knowing about it or following it, well, that's a different story). – thrig Jan 28 '16 at 17:47
  • So there isn't a designated section of the man page for this sort of thing? And there isn't something I can search for in regards to this? – leeand00 Jan 28 '16 at 17:48
  • 1
    @leeand00 it doesn't seem that way. All GNU coreutils appear to use Exit status, however rsync uses EXIT VALUES. – jordanm Jan 28 '16 at 17:50
  • Well as long as I can get the value I guess it doesn't matter as long as the value isn't the same for two different results; and that's where the documentation would help. – leeand00 Jan 28 '16 at 17:50
  • Some commands (legacy vi(1) is notorious) just return a random exit code, regardless of exit/failure. And "random" is really random, I believe it was just what was left in a VAX register by whatever code ran last before exit. – vonbrand Jan 28 '16 at 22:16