簡體   English   中英

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=$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

發生這種情況的唯一方法是您的 shell 阻止SIGINT 根據您的描述,您的外殼似乎可以做到。 重置 shell 中的SIGINT以便您的腳本接收SIGINT

在 shell 中運行以下命令並運行腳本:

trap - SIGINT

嘗試:

stty intr "^C"

如果這不起作用,請嘗試:

stty -a

並找出您的設置有什么問題。 如果不能,請使用 stty -a 的輸出更新您的答案。

還要確保您沒有像其他答案中提到的那樣捕獲中斷信號 (2)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM