我正在開發一個管理一些陷阱的腳本。一開始,我只設法INT和SIGTSTP與此代碼和它工作得很好:Bash陷阱,捕獲並將它們作爲相同函數的參數
#!/bin/bash
function capture_traps() {
echo -e "\nDoing something on exit"
exit 1
}
trap capture_traps INT
trap capture_traps SIGTSTP
read -p "Script do its stuff here and we use read for the example we pause for time to generate trap event"
exit 0
然後我試圖添加新的陷阱我要管理,這是SIGINT和SIGHUP。在第一種情況下我這樣做(這是工作):
#!/bin/bash
function capture_traps() {
echo -e "\nDoing something on exit"
exit 1
}
trap capture_traps INT
trap capture_traps SIGTSTP
trap capture_traps SIGINT
trap capture_traps SIGHUP
read -p "Script do its stuff here and we use read for the example we pause for time to generate trap event"
exit 0
於是,我決定就取決於陷阱的退出做不同的東西,我不想爲每一個創建不同的功能。我知道在bash中,你可以循環使用for item in [email protected]; do
命名法的一個函數的參數,所以我嘗試了,但它似乎沒有工作試圖區分這種陷阱。我使這個代碼不起作用。
#!/bin/bash
function capture_traps() {
for item in [email protected]; do
case ${item} in
INT|SIGTSTP)
echo -e "\nDoing something on exit"
;;
SIGINT|SIGHUP)
echo -e "\nDoing another thing even more awesome"
;;
esac
done
exit 1
}
trap capture_traps INT SIGTSTP SIGINT SIGHUP
read -p "Script do its stuff here and we use read for the example we pause for time to generate trap event"
exit 0
任何幫助?必須有僅使用一個功能適用於所有的陷阱,以提高我的代碼的方式,但我不知道怎麼...