linux – 中断’read -s’后隐藏的终端输入
作者:互联网
在等待来自操作的输入时按Ctrl-C读取-sp将操作返回到命令行,但给定的输入被隐藏,就像它仍在运行read -s一样.
例
#!/bin/sh
sig_handler() {
echo "SIGINT received"
exit 1
}
trap "sig_handler" SIGINT
read -sp "ENTER PASSWORD: " password
echo
echo $password
通常执行如下:
$~ ./example.sh
ENTER PASSWORD:
password
$~ text
-bash: text: command not found
但是如果你在ENTER PASSWORD按Ctrl-C就可以了
$~ ./example.sh
ENTER PASSWORD: SIGINT received
$~ -bash: text: command not found
在您使用重置刷新之前,文本或任何其他后续命令不会显示为输入.
收到SIGINT后如何将文本返回正常输入?出于明显的安全原因,不需要-p“输入密码:”密码.
解决方法:
将stty sane添加到信号处理程序,以便将终端恢复到其默认状态:
sig_handler() {
echo "SIGINT received"
stty sane
exit 1
}
标签:linux,sh,stty 来源: https://codeday.me/bug/20190523/1154572.html