Exit status, $?, and term status

Exit status, $?, and term status

Post by John Nola » Wed, 02 Jun 1999 04:00:00



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

 
 
 

Exit status, $?, and term status

Post by Justin Vallo » Sat, 05 Jun 1999 04:00:00



> 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

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:

EXIT STATUS
       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.

--
-Justin


 
 
 

Exit status, $?, and term status

Post by James Youngma » Mon, 07 Jun 1999 04:00:00



> 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).  

Actually, it's 128 + the signal number.

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

--

 
 
 

1. Exit status in trapped exit function

Hello everybody,

I'm trying to write an exit-handler function in korn-shell (ksh) which
takes in account the exit-status. It seems that I cannot use the ${?}
variable for this. It all comes down to the ExitHandler function and the
"exit 1" command :

   #!/usr/bin/ksh
   function ExitHandler
   {
      echo "Exit status is ${?}"
      exit
   }

   trap 'ExitHandler' EXIT
   exit 1

But when I execute this script (it's called _tryout), I always get exit
status 0.

   $ Divs/_tryout
   Exit status is 0

I have tried some syntax variations but nothing helps. Any idea anybody ?

Thank in advance,
real

2. A free DOS from anywhere?

3. should xargs exit on non-zero program exit status?

4. ipchain and logging?

5. AMD status map status update solved!

6. LINUX SUCKS!!!!

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

8. Port denyed to localhost????

9. Move "used FPU status" into new non-atomic thread_info->status field.

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

11. Move "used FPU status" into new non-atomic thread_info->status field.

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

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