1
如何在bash中使用多個退出陷阱? 說我想在退出-1上的退出代碼1 和退出-2退出代碼2BASH不同退出級別的多個退出函數
function on-exit1 {
echo "do stuff here if code had exit status 1"
}
function on-exit2 {
echo "do stuff here if code had exit status 2"
}
.....
trap on-exit1 EXIT # <--- what do i do here to specify the exit code to trap
trap on-exit2 EXIT # <--- what do i do here to specify the exit code to trap
.....
some bashing up in here
blah...blah
exit 1 # do on-exit1
else blah blah
exit 2 # do on-exit2
else blah blah
exit N # do on-exitNth
你爲什麼要捕捉退出,在功能只是出口,並呼籲他們不是退出的? – 123
或者像@ 123所提到的那樣做,或者定義一個退出函數並檢查退出代碼的'$?'。 – infotoni91
'exit_check EXIT'和'exit_check(){status =「$?」; [「$ status」-eq 1] && on_exit1 && return 0; [「$ status」-eq 2] && on_exit2 && return 0; }' – Aserre