Ctrl-C 停止 shell 脚本执行
Ctrl-C to stop the shell script execution
我有一个shellscript如下。这不会在按 Ctrl-C 时终止。你能指导我如何修改以下代码以终止 Ctrl-C 作为输入的执行。
#!/bin/bash
validateURL()
{
regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
string=
if [[ $string =~ $regex ]]
then
echo "0"
else
echo "1"
fi
}
RED='3[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
发生这种情况的唯一方法是您的 shell 阻止 SIGINT
。根据您的描述,您的 shell 似乎可以做到。重置 shell 中的 SIGINT
以便您的脚本接收 SIGINT
.
运行 您的 shell 和 运行 脚本中的以下内容:
trap - SIGINT
尝试:
stty intr "^C"
如果不行,试试:
stty -a
并找出您的设置有什么问题。如果不能,请使用 stty -a.
的输出更新您的答案
还要确保您没有捕获其他答案中提到的中断信号 (2)。
我有一个shellscript如下。这不会在按 Ctrl-C 时终止。你能指导我如何修改以下代码以终止 Ctrl-C 作为输入的执行。
#!/bin/bash
validateURL()
{
regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
string=
if [[ $string =~ $regex ]]
then
echo "0"
else
echo "1"
fi
}
RED='3[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
发生这种情况的唯一方法是您的 shell 阻止 SIGINT
。根据您的描述,您的 shell 似乎可以做到。重置 shell 中的 SIGINT
以便您的脚本接收 SIGINT
.
运行 您的 shell 和 运行 脚本中的以下内容:
trap - SIGINT
尝试:
stty intr "^C"
如果不行,试试:
stty -a
并找出您的设置有什么问题。如果不能,请使用 stty -a.
的输出更新您的答案还要确保您没有捕获其他答案中提到的中断信号 (2)。