[英]Ctrl-C to stop the shell script execution
I have a shellscript as follows.我有一个如下的shellscript。 This doesn't terminates on pressing Ctrl-C.
这不会在按 Ctrl-C 时终止。 Can you guide me on how to modify the following code to kill the execution on Ctrl-C as input.
你能指导我如何修改以下代码以终止 Ctrl-C 作为输入的执行。
#!/bin/bash
validateURL()
{
regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
string=$1
if [[ $string =~ $regex ]]
then
echo "0"
else
echo "1"
fi
}
RED='\033[0;31m'
echo -n "Enter the URL :"
while read URL_REGISTRY
do
if [ $(validateURL $URL_REGISTRY) == "0" ]
then
break
else
echo -e "${RED}Wrong URL entered."
tput sgr0
echo -n "Enter the URL again :"
fi
done
The only way this can happen is if your shell blocks SIGINT
.发生这种情况的唯一方法是您的 shell 阻止
SIGINT
。 As per your description, your shell seems to do it.根据您的描述,您的外壳似乎可以做到。 Reset the
SIGINT
in your shell so that your script receives SIGINT
.重置 shell 中的
SIGINT
以便您的脚本接收SIGINT
。
Run the following in your shell and run the script:在 shell 中运行以下命令并运行脚本:
trap - SIGINT
Try:尝试:
stty intr "^C"
If that does not work, try:如果这不起作用,请尝试:
stty -a
and figure out what is wrong with your settings.并找出您的设置有什么问题。 If you can't, update your answer with the output of stty -a.
如果不能,请使用 stty -a 的输出更新您的答案。
Also make sure that you have not trapped the interrupt signal (2) as mentioned in the other answers.还要确保您没有像其他答案中提到的那样捕获中断信号 (2)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.