문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top