我正在开发一个管理一些陷阱的脚本。一开始,我只设法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
任何帮助?必须有仅使用一个功能适用于所有的陷阱,以提高我的代码的方式,但我不知道怎么...