[Toybox] Testing: how to check return code (Rob Landley)

Rob Landley rob at landley.net
Wed Nov 6 03:28:32 PST 2019


On 11/6/19 3:34 AM, scsijon wrote:
> from the (matching) table in the wikka>
> 
> Exit Code Number    Meaning    Example    Comments
> 
> 1    Catchall for general errors    let "var1 = 1/0"    Miscellaneous errors,
> such as "divide by zero" and other impermissible operations

Not helpful.

> 2    Misuse of shell builtins (according to Bash documentation) empty_function()
> {}    Missing keyword or command, or permission problem (and diff return code on
> a failed binary file comparison).

We already know that "false" and "error" are sometimes distinguished by this,
but commands that _don't_ distinguish don't return 2 for error so it's not
generally useful.

> 126    Command invoked cannot execute    /dev/null    Permission problem or
> command is not an executable

So my guess was approximately right, but it's a bash-ism.

$ chmod +x PastedGraphic-2.png
$ ./PastedGraphic-2.png
./PastedGraphic-2.png: line 1: $'\211PNG\r': command not found
./PastedGraphic-2.png: line 2: $'\032': command not found
./PastedGraphic-2.png: line 3: syntax error near unexpected token `newline'
IHDR�4�k�sRGB��
�biTXtXML:com.adobe.xmp<x:xmpmeta xmlns:x="adobe:ns:meta/"
x:xmptk="XMP Core 5.4.0">'
$ echo $?
2

And on top of that, it's _fundamentally_ useless.

> 127    "command not found"    illegal_command    Possible problem with $PATH or
> a typo

Yes, I knew that.

> 128    Invalid argument to exit    exit 3.14159    exit takes only integer args
> in the range 0 - 255 (see first footnote)

Not usefull

> 128+n    Fatal error signal "n"    kill -9 $PPID of script    $? returns 137
> (128 + 9)

Already knew it.

> 130    Script terminated by Control-C    Ctl-C    Control-C is fatal error
> signal 2, (130 = 128 + 2, see above)

Not useful _and_ already knew it.

> 255*    Exit status out of range    exit -1    exit takes only integer args in
> the range 0 - 255

Should basically never happen.

> (sorry for the format, but that's the way it copied)

It's neither a standard, nor particularly helpful.

It's nice that this 20 year old document is in a wiki now, but it hasn't changed
since then and the bash scripting guide is documenting bashisms from the 2.x
era, which I'm already examining while doing shell stuff.

Rob



More information about the Toybox mailing list