It is currently Tue, 07 Dec 2021 03:34:25 GMT

Author Message
 Exit status, $?, and term status
How can I tell whether $? contains the actual exit status
of a program or its termination signal and coredump flag?

For example:  

[A] If I run "ls -l zzzz", where the file zzzz does
not exist, then $? contains "2".  This is the exit
status of ls.

[B] However, if I run a small compiled C program like this

  int main() {

     int i;
     i = 1/0;  /* Floating point exception, signal 8 */

....then $? contains "136". 136 is not actually
the exit status, it is the signal ANDed with
the coredump flag.  (128 && 8, or in hex: 0x80 && 0x08).  

Or, to rewrite the term status in binary:

[A]  term status = 0000 0010 0000 0000
[B]  term status = 0000 0000 1000 1000

In [A], the shell variable $? contains 2, which is
the upper byte of the termination status (i.e.,
the exit status).  
But in [B], $? contains 136, which is the lower byte
of the termination status.  This is not the exit status.

For an aribtrary value of $?, how can you tell which one
it contains?  Is there any exhaustive documentation
somewhere, which explains *exactly* how the value of $?
is derived, and how one should interpret its contents?

Thanks for any assistance.

--John Nolan

 Sat, 17 Nov 2001 03:00:00 GMT   
 Exit status, $?, and term status

As you probably know, these values are portably checked (in C) via the
wait-macros: WIFEXITED(status), WEXITSTATUS(status),
WIFSIGNALED(status), WTERMSIG(status).  See man wait.

The value that appears in $? is put there by the shell.  Check bash,
tcsh, sh, etc, for the rules.  bash indirectly describes what appears
in $? via its "Exit Status" section:

       For the purposes of the shell, a command which exits  with
       a  zero exit status has succeeded.  An exit status of zero
       indicates success.  A non-zero exit status indicates fail-
       ure.   When  a  command terminates on a fatal signal, bash
       uses the value of 128+signal as the exit status.

       If a command is not found, the child  process  created  to
       execute it returns a status of 127.  If a command is found
       but is not executable, the return status is 126.

       Bash itself returns the exit status of  the  last  command
       executed,  unless  a syntax error occurs, in which case it
       exits with a non-zero value.  See also  the  exit  builtin
       command below.


 Tue, 20 Nov 2001 03:00:00 GMT   
 Exit status, $?, and term status

Actually, it's 128 + the signal number.

if test $rv -gt 128
        sig=`expr $rv - 128`
        echo "Terminated by signal $sig"
elif test $rv -eq 127
        echo "Command not found"
        echo "Exited with status $rv"

ACTUALLY reachable as @free-lunch.demon.(whitehouse)

 Thu, 22 Nov 2001 03:00:00 GMT   
   [ 3 post ] 

Similar Threads

1. Exit status in trapped exit function

2. eth0 transmit timed out status 0000 phy status 782d

3. D-Link DFE530TX: Transmit timed out, status 0000, PHY status 782d

4. eth0 : transmit timed out, status 0000, PHY status 782d, resetting

5. Couldn't open status file /var/lock/samba/STATUS....LCK

6. eth0:transmit timed out, tx-status 00 status e000

7. /dev/hdc status error: status=0x01 {Error}

8. AMD status map status update solved!

9. Sep 22 15:00:06 woohoo inetd[67]: /usr/sbin/nmbd: exit status 0x1

10. exit status from pppd / chat

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software