0

我有一个脚本:使用 PUTTY 会话运行的 giga.sh(shell 脚本 BASH)。

我看到大多数人喜欢使用“Control+C”复制并粘贴为“Control+V”。我的一些聪明的朋友使用鼠标右键单击然后复制,然后粘贴,我想知道,他们不知道 Control+C 吗?由于他们的经验,他们实际上是对的 :),就像当 giga.sh 为 PRODUCTION 部署/任务运行时那样。

我想要的是:如果 giga.sh 正在运行并且有人打开了多个应用程序,并且他们定期执行复制/粘贴操作以从这些打开的应用程序中复制文本/图像,那么如果鼠标控制意外出现在 PUTTY 窗口/会话,然后执行“Control+C”不应该杀死正在运行的 giga.sh 脚本。

现在我的脚本在 giga.sh 中捕获了几个信号(2 即 INT,15 即 TERM(目前默认))。

这是否意味着,我所要做的就是从以下内容中删除“2”(请参阅​​陷阱...行),它会使正在运行的 giga.sh 脚本免受用户意外按下 control+c 键的影响?我认为它不会,因为那时我们不会捕获任何 sig 2/INT,想知道是否有人有更好的主意。新的 trap / trap_mesg() 分别处理 sig 2 和 sig 15,会这样做吗?

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
##################################
4

1 回答 1

0

我假设它会是这样的:

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
##################################
于 2013-06-19T15:13:18.223 回答