일부 트랩을 관리하는 스크립트를 개발 중입니다. 처음에 나는 단지이 코드 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
도움이 필요하십니까? ...이 모든 함정에 대해 하나의 기능을 사용하여 내 코드를 향상시킬 수있는 방법이 있어야합니다,하지만 난 방법을 모른다