custom trap signal in ksh
-
11-10-2019 - |
Pergunta
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?
Solução
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
Outras dicas
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?