我有一个脚本: giga.sh (shell script BASH),它使用PUTTY会话运行。
我发现大多数人喜欢使用"Control+C“复制并粘贴为"Control+V”。我的几个聪明的朋友使用鼠标右键点击,然后复制,然后粘贴,我想知道,他们不知道Control+C吗?由于他们的经验,他们实际上是正确的:),就像在giga.sh运行生产部署/任务时那样做。
我想要的是:如果giga.sh正在运行,并且有人打开了多个应用程序,并且他们定期执行复制/粘贴操作来从这些打开的应用程序中复制文本/图像,那么如果鼠标控件意外出现在PUTTY窗口/会话中,那么执行"Control+C“就不应该,杀死正在运行的giga.sh脚本。
现在,我的脚本在giga.sh中捕获了几个信号(2个,即INT,15个,即TERM (默认))。
这是否意味着,我所要做的就是从以下代码中删除"2“(参见trap...line),这将使正在运行的giga.sh脚本对用户意外按下control+c键具有免疫力?我假设它不会,因为我们不会捕获任何sig 2/INT,不知道是否有人有更好的想法。新的陷阱/ trap_mesg()来分别处理sig2和sig15,可以做到吗!?
trap_mesg ()
{
#...do something here..
#...before actually exiting out...
#...do something here..
}
#####################################
## Trap signals : INT, TERM. catch ##
#####################################
#Set NULL/Blank value to trap_call. This variable will help in running trap_mesg only once during the life of this script.
trap_call="";
trap 'if [ -z ${trap_call} ]; then trap_call="1"; trap_mesg ; fi' 2 15
##################################发布于 2013-06-19 23:13:18
我假设它是这样的:
trap_mesg_sig2 ()
{
#...do something here..
#...Don't exit and warn the user that if he really want to KILL -2/INT
#...pass some Signal to script PID to continue as normal/resume instead of getting killed
# also reset trap_mesg_sig2 varialble back to "".
#...maintain a counter i.e. if user presses Control+C (kill -2/INT) 3 times, only then
# exit.
#...do something here..
}
#####################################
## Trap signals : INT, TERM. catch ##
#####################################
#Set NULL/Blank value to trap_call. This variable will help in running trap_mesg only once during the life of this script.
trap_call_sig2="";
trap 'if [ -z ${trap_call_sig2} ]; then trap_call_sig2="1"; trap_mesg_sig2 ; fi' 2
##################################
trap_mesg_sig15 ()
{
#...do something here..
#...before exiting out...
#...do something here..
}
#####################################
## Trap signals : INT, TERM. catch ##
#####################################
#Set NULL/Blank value to trap_call. This variable will help in running trap_mesg only once during the life of this script.
trap_call_sig15="";
trap 'if [ -z ${trap_call_sig15} ]; then trap_call_sig15="1"; trap_mesg ; fi' 15
##################################https://stackoverflow.com/questions/17176585
复制相似问题