Re: shell error codes

From: Stachu 'Dozzie' K. (dozzie_at_dynamit.im.pwr.wroc.pl.nospam)
Date: 11/07/04


Date: Sun, 7 Nov 2004 20:03:48 +0000 (UTC)

On 2004-11-07, gregg wrote:
> 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"

Huh, that's funny:

#v+
[dozzie@hans dozzie]$ grep ENOENT /usr/include/ -R | grep define
/usr/include/asm/errno.h:#define ENOENT 2 /* No such file or directory */
#v-

#v+
[dozzie@hans dozzie]$ cat errno.c
#include <stdio.h>
#include <errno.h>

int main(void)
{
  printf("%d\n", ENOENT);
  return 0;
}
[dozzie@hans dozzie]$ gcc errno.c -o errno
[dozzie@hans dozzie]$ ./errno
2
[dozzie@hans dozzie]$
#v-

Where have you get that ENOENT == 127 from?

Let's go farther:

#v+
[dozzie@hans dozzie]$ cd qwertyuiop
cd: no such file or directory: qwertyuiop
[dozzie@hans dozzie]$ echo $?
1
[dozzie@hans dozzie]$ echo $ZSH_VERSION
4.0.7
[dozzie@hans dozzie]$ - ksh
[dozzie@hans dozzie]% cd qwertyuiop
ksh: cd: /home/dozzie/qwertyuiop - No such file or directory
[dozzie@hans dozzie]% echo $?
1
[dozzie@hans dozzie]% echo $KSH_VERSION
@(#)PD KSH v5.2.14 99/07/13.2
[dozzie@hans dozzie]$ - bash
[dozzie@hans dozzie]$ cd qwertyuiop
-bash: cd: qwertyuiop: No such file or directory
[dozzie@hans dozzie]$ echo $?
1
[dozzie@hans dozzie]$ echo $BASH_VERSION
2.05b.0(1)-release
[dozzie@hans dozzie]$
#v-

All my shells return 1 after try to cd into nonexistent directory.

#v+
[dozzie@hans dozzie]$ ls -l ipsec
-rw-r--r-- 1 dozzie users 7 Oct 12 14:57 ipsec
[dozzie@hans dozzie]$ cd ipsec
cd: not a directory: ipsec
[dozzie@hans dozzie]$ echo $?
1
[dozzie@hans dozzie]$ ./ipsec
zsh: permission denied: ./ipsec
[dozzie@hans dozzie]$ echo $?
1
[dozzie@hans dozzie]$
#v-

zsh returns 1 after any unsuccesful operation.

To sum up, it can be sometimes impossible to tell what went wrong.

> 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).
> Heck. I just wanted to understand what went wrong with error code
> returned to the shell (not specific values returned by external
> programs, which, as you have rightfully stated, may be whatever the
> programmer decided it would; but those values the shell itself uses when
> not finding a command, a file, or whatever...)

Then you need to look into the source code of the shell, I think.

> I suppose I'll have to read a bit of that Posix.1 paper just to get the
> application Error value <---> ERRNO name
> For I couldn't find it in the manpage (man 3 errno lists E codes all
> right, but without giving their actual int value)

Follow the /usr/include/errno.h file. I can be wrong, but these #defines
are not forced by POSIX to be any concrete value.

-- 
Stanislaw Klekot


Relevant Pages

  • Re: wget + redirect = overwrite?
    ... # echo $SHELL ... # cat siteid_list ... [dozzie@rudolf dozzie]$ cat siteid_list ...
    (comp.unix.shell)
  • Re: string formating
    ... >> I am referring to a bash script. ... >> I would like to create in a loop a string variable containing in turn ... [dozzie%albercik dozzie]$ echo $ZSH_VERSION ...
    (comp.unix.shell)
  • Re: Array saving
    ... > echo doesn't output a string, it outputs string with ANSI C ... [dozzie@hans dozzie]$ echo $BASH_VERSION ... Specification doesn't say that echo *must* expand the escape sequences, ...
    (comp.unix.shell)
  • Re: strip characters from string
    ... [dozzie@jarowit dozzie]$ cat> a.sh ... echo "$" ... Stanislaw Klekot ...
    (comp.unix.shell)
  • Re: Ignoring trap command for a grep function
    ... "Stachu 'Dozzie' K." ... >> echo not found ... therefore no trap will catch us. ... This is a signature virus. ...
    (comp.unix.shell)