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



 
Author Message
 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



 Sat, 10 Aug 2002 03:00:00 GMT   
 Exit status in trapped exit function
In article <MPG.131c92231a8b4bea989...@news.surfnet.nl>,

Chalk this up as another difference between ksh and pdksh.  Pdksh
prints the exit status.

--
Dan Mercer
damer...@uswest.net

Opinions expressed herein are my own and may not represent those of my employer.



 Sat, 10 Aug 2002 03:00:00 GMT   
 Exit status in trapped exit function
In article <88u7c3$lb...@magnum.mmm.com>,
  damer...@mmm.com (Dan Mercer) wrote:
I don' think there is any ksh/pdksh differences here.
Your code works but you are not using it correct!

Your program, _tryout, has these "parts"
o) The first line that gives the program to run
o) the function definition
o) the trap command
o) and exit statement
When you run the program the status variable, "${?}", NEVER gets set
to any value other than zero 0.  Why? you have no errors!  Your code
works.  It does what you tell it to.  Here are two suggestions to see
what is going on.
1)
        $Divs/_tryout
        Exit status is 0  # should be printed
        echo ${?}         # should print "1"
    Why? the "called shell", $Divs/_tryout, has not errors but the
        calling shell, the one you are running interactively, has gotten
        an error exit status when it ran the $Divs/_tryout program

2) In your code add one more line
        /bin/cat /does-not-exist
        exit 1
    You should see
        $Divs/_tryout
        cat: no such file or directory # printed to stderr
        Exit status is 1
        echo ${?} # just to see, it should be 1

Your trap program, "ExitHandler", is designed to print the most
recent value of ${?} and then exit 0, always it will exit 0.
Here is a suggestion for chaninging it:

function ExitHandler
{
    # first thing, grab the exit status of the last
    stat=${?} # command the was run be for the trap was called
    echo "Exit status is ${stat}" # can be printed, examined, etc.
    exit ${stat} # make _tryout exit with the same value as the command
                 # of interest

For the above to have ANY value other than zero, then some error must
occur within your program, otherwise it always exists  zero.

There is a shell script, ftp://ftp.opensysmon.com/pub/shells/new
that contains something else, it may be what you want.

It seems to me that what you really want to do is to trap the value
that your program exits with and print it.  I call that the "doexit"
trap.  The "new" shell shows the code.

The idea is like this:

function doexit {
   print Exit value is ${1:-0} # exit $1, default to 0
   exit ${1:-0}

trap "doexit 1" 1     # exit 1 if hit with kill -1
trap "doexit 2" 2     # exit 2 on interrupt
trap "doexit 15" 15   # exit 15 on terminate signal
# OR SYMBOLICALLY
trap "doexit 1" HUP
trap "doexit 2" INTR
trap "doexit 15" TERM  # you can all so modify doexit so it accepts
                       # symbolic arguments
--
Help for UNIX admins!  Free shell archive at
http://www.opensysmon.com/support
While you're at it, why not give OpenSysMon a try?
Low cost, high quality, monitor systems, shells and cron.

Sent via Deja.com http://www.deja.com/
Before you buy.



 Sat, 10 Aug 2002 03:00:00 GMT   
 Exit status in trapped exit function
You are mistaking the problem.

   exit 1

set $? to 1.  The problem is that real ksh executes the exit handler,
then sets the return value.  Pdksh sets the return value then
executes the exit handler.

--
Dan Mercer
damer...@uswest.net

In article <88ufar$6g...@nnrp1.deja.com>,

Opinions expressed herein are my own and may not represent those of my employer.



 Sat, 10 Aug 2002 03:00:00 GMT   
 Exit status in trapped exit function
I believe you always get 0 as the value of $? in your exit handler because
your last command (namely, "exit 1") always succeeds.  The exit command sets
the exit status of the Korn shell in which it is used, not the value of $?
within that shell.  If you change "exit 1" to something like
"a_command_that_fails" your exit handler will report a non-zero exit status.

Perhaps an acceptable workaround would be to assign the desired exit status
to a shell variable before exiting, and then refer to that shell variable in
your exit command and in your exit handler.  E.g.,

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

trap 'ExitHandler' EXIT

ExitStatus=1
exit $ExitStatus

Hope this helps.

Mark



 Sat, 10 Aug 2002 03:00:00 GMT   
 
   [ 5 post ] 

Similar Threads

1. trap and exit status

2. exit status inside trap

3. sh: trap 0 and exit status

4. catching exit status of a function and use it later on in the script

5. Exit status, $?, and term status

6. telnet exits (crashes) after exiting Emacs

7. howto make pppd exit when remote *ppp* exit(0)

8. Exit nn via trap Q

9. console unreadable after X exit/videomode on X exit problem


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