Is there a way for me to implement custom fake signals in ksh? Currently am capturing the ERR signal and exiting. However, due to a change, there are calls that may not return success, however that is a valid condition. In such case, I want to make sure that this call generates a different signal or handle the ERR differently. Is there a way to do that?
Asked
Active
Viewed 2,770 times
2 Answers
2
You can use kill
to send any signal you want to the current shell. You can use exit
in a subshell or return
in a function to set any error code you want.
Try this script:
#!/bin/ksh
trap 'echo USR1 signal processed' USR1
trap 'echo ERR signal processed' ERR
[[ $1 == a ]] && kill -s USR1 $$ || (exit 1)
echo "done"
Example:
$ ./testsignal
ERR signal processed
done
$ ./testsignal a
USR1 signal processed
done

Dennis Williamson
- 346,391
- 90
- 374
- 439
-
Hmm, HI Dennis, interestingly this isnt working. I only see the echo done above and dont see the USR1 being caught. In the above case, even ERR is not caught. However, when I added the following chmod 777 / where I dont have permissions, it printed 'ERR signal processed' – Kiran Dec 17 '10 at 13:28
0
Perhaps you could simply wrap the statements whose exit codes you want to treat specially in a construction that would simply do what you want (employing "or" or "if not" constructions and analyzing the exit codes).
That seems to be a much cleaner programming style, doesn't it?

imz -- Ivan Zakharyaschev
- 4,921
- 6
- 53
- 104
-
:) thats what I am doing right now! wanted to clean up further and based on what I have, trapping signals into a few functions seemed to be the cleanest way – Kiran Dec 16 '10 at 22:47