ksh: if I trap on exit to rm tmp files, how do I save return value ?

ksh: if I trap on exit to rm tmp files, how do I save return value ?

Post by Michael Ratn » Fri, 23 Jul 1993 00:54:22



Hi !

I have the following (sample) script:

------------------------------------------
#!/bin/ksh

echo hi >/tmp/hi

trap "if [ -s /tmp/hi ];then rm /tmp/hi;exit; fi" 0 1 2 3

exit 1
------------------------------------------

Well, this script returns 0, while I want it to return 1.  It seems
that $? becomes 0 when "rm" succeeds.  ???

                                                        -- Mike Ratner

--
*****************************************************************************
Michael B. Ratner  Associate Programmer, Advanced Network & Services (ANS)

"The Road goes ever on and on..."  - J.R.R. Tolkien

 
 
 

ksh: if I trap on exit to rm tmp files, how do I save return value ?

Post by Dan Spee » Fri, 23 Jul 1993 03:54:07


|>
|> Hi !
|>
|> I have the following (sample) script:
|>
|> ------------------------------------------
|> #!/bin/ksh
|>
|> echo hi >/tmp/hi
|>
|> trap "if [ -s /tmp/hi ];then rm /tmp/hi;exit; fi" 0 1 2 3
|>
|> exit 1
|> ------------------------------------------
|>
|> Well, this script returns 0, while I want it to return 1.  It seems
|> that $? becomes 0 when "rm" succeeds.  ???
|>
|>                                                   -- Mike Ratner

  The exit in your trap statement is the source of the return code.
  Either set different traps with various return codes or don't trap
  for 0.

--
--------------------------------------------------------------------------
Daniel E. Speers IV
First Boston Corp.

609-243-0754
--------------------------------------------------------------------------
My views are not for sale, lease, or rent unless there is money to be had.
--------------------------------------------------------------------------

 
 
 

ksh: if I trap on exit to rm tmp files, how do I save return value ?

Post by David W. Tamk » Fri, 23 Jul 1993 05:44:47



| #!/bin/ksh
|
| echo hi >/tmp/hi
|
| trap "if [ -s /tmp/hi ];then rm /tmp/hi;exit; fi" 0 1 2 3
|
| exit 1
| ------------------------------------------
|
| Well, this script returns 0, while I want it to return 1.  It seems
| that $? becomes 0 when "rm" succeeds.  ???

Change the trap to

trap "if [ -s /tmp/hi ];then rm /tmp/hi;exit 1; fi" 0 1 2 3

David W. Tamkin  Box 3284  Skokie, Illinois  60076-6284  312-714-5610

 
 
 

ksh: if I trap on exit to rm tmp files, how do I save return value ?

Post by Michael Ratn » Sat, 24 Jul 1993 06:48:12


Thanks for all the replies that I got.  The prize for the most elegant
solution goes to...

[drum beat]


who simply suggested to remove the word "exit" from the trap
statement.

                                        cheers,

                                                        -- Mike Ratner
--
******************************************************************************
                        |  Michael B. Ratner  |
                        |_____________________|

 
 
 

1. ksh: trap '...' exit int ... or just trap '...' exit?

trap '...' exit int ...

int will cause the script to exit, so '...' get run twice:


#!/bin/ksh

trap 'print a' exit int
sleep 10

^Ca
a

But the natural exit will always causes '...' to run,
which is often not desired.

So should we always write something like

trap 'print a' exit int
...
trap - exit
exit 0
?
--
Michael Wang
http://www.unixlabplus.com/

2. WD8003E ethernet card?

3. What is this supposed to return: "trap 'exit ' 0; exit"?

4. ~In the News for May22~

5. find /tmp -atime +1 | rm does not delete my old tmp files :-(

6. help, erased files

7. trap "action; exit" signals = trap "action" exit?

8. Alpha newbie needing help!

9. Ksh:Loggin StdOut/StdErr to file and screen and keep exit value

10. can return values be trapped in a makefile

11. trapping and exit values

12. HELP:Trapping return values

13. can makefile trap the return values