shell Trap命令用法
阿新 • • 發佈:2018-11-17
image extra files aaa all med hello fun per
[root@localhost prog]# cat trap2.sh #!/bin/bash function cleanup() { echo "Received signals and cleanup files" } #trap 到SIG信號,自動停止 trap ‘cleanup;exit 1‘ SIGHUP trap ‘cleanup;exit 1‘ SIGINT trap ‘cleanup;exit 1‘ SIGTRAP #SIGKILL can not be traped while : do echo "Hi there" sleep 1 done [root@localhost prog]#
[root@localhost prog]# cat trap4.sh #!/bin/bash trap ‘cleanup‘ SIGINT function cleanup() { echo "call function" trap - SIGINT #遇到一次SIGINT, 就會清除掉trap捕捉SIGINT } while : do echo "hello aaa" sleep 2 done
[root@localhost prog]# cat trap5.sh #!/bin/bash cleanup_new() { echo "Some new and extra cleanup performed" trap - SIGINT #第三次TRAP 加exit。消除SIGINT } cleanup_old() { echo "Some cleanup performed" trap cleanup_new SIGINT #第二次繼續trap INT } trap ‘cleanup_old SIGINT‘ SIGINT #第一次trap SIGINT while true do echo ‘hello‘ sleep 1 done [root@localhost prog]#
#!/bin/bash trap "echo trap ctrl+c" SIGINT while : do echo "Hi there" sleep 1 done #通過kill -l 命令可以看到,ctrl+C等同SIGINT,不會打斷,但是會trap到所以顯示trap ctrl+c
shell Trap命令用法