BASH:Ctrl + C键输入时打破当前的终端 [英] BASH: Ctrl+C during input breaks current terminal

查看:549
本文介绍了BASH:Ctrl + C键输入时打破当前的终端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Bash的版本是: GNU bash的版本4.3.11(1) - 释放下(x86_64-PC-Linux的GNU)

My Bash version is: GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu).

我有这样一块code的:

I have a piece of code like this:

   while true; do
        echo -n "Set password: "
        read -s pass1
        printf "\n"
        echo -n "Repeat password: "
        read -s pass2
        printf "\n"
        if [ $pass1 != $pass2 ]; then
            echo "Not same."
        else
            break
        fi
    done

如果我同时上线阅读退出此脚本 CTRL + C PASS2 我的终端停止正常工作。

If I exit this script with ctrl+c while on the line reading pass2 my terminal stops working properly.

user@host:~/bin$ user@host:~/bin$ user@host:~/bin$ user@host:~/bin$No command 'aaaa' found, did you mean...

一切都被写入同一行,我的打字不回应。我用腻子为这些连接。当我连接到主机再次everyting重新工作。

Everything gets written to the same line and my typing is not echoed. I use Putty for these connections. When I connect to host again everyting is working again.

当某些条件为真这个问题才会出现:
只有当它与须藤运行。无论普通用户和根(根成了与须藤-S )可以毫无问题退出脚本。

This problem only appears when certain conditions are true: Only if it is run with sudo. Both normal user and root (became root with sudo -s) can exit the script without problem.

仅在第二读语句存在此问题。而读退出 PASS1 变量会导致没有问题。

This problem exists only during second read statement. Exiting while reading pass1 variable causes no trouble.

可能是什么这个问题的原因是什么?

What could be the reason for this problem?

推荐答案

重置你的终端应该有所帮助。 读-S 正在改变一些设置来隐藏输入,并在某些情况下,收到中断信号后,不会恢复这些设置。该行添加到脚本的开头,以确保您的脚本终端复位到一个良好的状态:

Resetting your terminal should help. read -s is changing some settings to hide the input, and in some cases these settings aren't restored after the interrupt signal is received. Add this line to the beginning of the script to make sure your script resets the terminal to a good state:

trap 'stty sane' INT

正如User112638726指出的那样,你倒是应该保存终端的状态,在脚本的开头,并恢复(而不是假设原始状态是相同的是什么理智生产)。

original_tty_state=$(stty -g)
trap "stty $original_tty_state" INT

这篇关于BASH:Ctrl + C键输入时打破当前的终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆