Re: shell error codes

From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 11/07/04


Date: 07 Nov 2004 20:31:30 GMT

2004-11-07, 20:40(+01), gregg:
> Barry Margolin wrote:
>
>> The shell doesn't report error codes, it reports the exit status of
>> programs. These are not standardized, except that 0 means success and
>> anything else is considered failure.
>
> I agree to that when a program is launched by the shell.
> But what of:
>
> gregg@darkstar [0] ~$ ./jkkkl
> bash: No such file or directory
> gregg@darkstar [127] ~$
>
> Here, "127" is for "no such file or directory", i.e "ENOENT"
>
> What I want is to associate those integer values with their errno
> counterpart. Some are C-ANSI defined, others Posix.1 (as I gather from
> manpages).
[...]

See:
http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_01

The only standardized exit codes are 126 and 127

SUS> If the execve() function fails due to an error equivalent to
SUS> the [ENOEXEC] error defined in the System Interfaces volume
SUS> of IEEE Std 1003.1-2001, the shell shall execute a command
SUS> equivalent to having a shell invoked with the pathname
SUS> resulting from the search as its first operand, with any
SUS> remaining arguments passed to the new shell, except that the
SUS> value of "$0" in the new shell may be set to the command
SUS> name. If the executable file is not a text file, the shell
SUS> may bypass this command execution. In this case, it shall
SUS> write an error message, and shall return an exit status of
SUS> 126.
[...]
SUS> If the search is unsuccessful, the command shall fail with an
SUS> exit status of 127 and the shell shall write an error message

Even the $? for signal termination are not standardized
SUS> The exit status of a command that terminated because it
SUS> received a signal shall be reported as greater than 128

(in most shells, it's 128+SIGNUM, in ksh93, it's 256+signum)

(anyway the signal values are not standardized).

So, you have, for values of $?

0: no error
126: program returned exit code 126 or execve(2) failed with
ENOEXEC
127: program returned exit code 127 or the command was not
found
128+: program returned exit code >128 or it was terminated by a
signal
255+: program was terminated by a signal (and shell behaves like
ksh93)

-- 
Stephane


Relevant Pages

  • Re: cant cleanly exit shell from FC5 rescue CD
    ... When I type in 'exit' and hit enter, ... type 'linux rescue' at the prompt and hit ENTER ... at the shell prompt, ...
    (Fedora)
  • funny SIGCHLD behavior
    ... I'm trying to implement a simple shell and am facing some ... SIGCHLD must get sent to the handler. ... to exit. ... * cmdbuf as required by execvfunctions ...
    (comp.unix.programmer)
  • Re: CreateProcess not working as doc specifies.
    ... > be properly maintained running through cmd.exe. ... Have you looked at the EXIT command? ... a smart wrapper for CreateProcess() will consider shell vs spawns. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: How to make a batch file to start program and close the oldwindow?
    ... 'nohup' takes care of an assortment of things that need to keep working. ... The 'nohup' redirects output to a file and starts a new process group, the '&' lets the shell continue instead of waiting, and the 'exit' should close the shell window. ...
    (Fedora)
  • Re: Changing directories, script, mc
    ... shell, it doesn't need to be executable. ... $chap1 konsole exits immediately ... appeared in the script, that would be what was doing it - you wouldn't ... would exit. ...
    (comp.os.linux)