trap "stty echo" INT 在与 read -s 一起使用时无效
trap "stty echo" INT has no effect when used with read -s
鉴于此 bash 脚本:
stty -echo
echo $(stty)
reset() {
stty echo
echo $(stty)
exit
}
trap reset int
read -s
sleep 10
我希望 echo 选项被启用,但是在按下 ctrlc 之后它仍然被禁用,即使我有 运行 stty echo
(正如您在 reset
函数的输出中看到的那样)。
正如评论中的@KamilCuk ,read
保存配置并在进程存在时恢复配置。这导致在 read
被 运行ning 时完成的修改被丢弃。解决方案是在 运行 宁 read
之前恢复默认值并在 read
完成后重做它们。
stty -echo
echo $(stty)
reset() {
stty echo
echo $(stty)
exit
}
trap reset int
stty echo # This line has been added
read -s
echo read finished
stty -echo # This line has been added
sleep 10
@JonathanLeffler 也 那
It can be useful to use old=$(stty -g)
to capture the current terminal settings, and then use stty "$old"
to reinstate those settings.
:
Using it allows you to reinstate the exact terminal settings when stty -g
was invoked. This can be more reliable than undoing changes with stty -echo
etc.
我认为这是更合适的解决方案,因为 stty
可能 运行 默认处于无回显模式。
鉴于此 bash 脚本:
stty -echo
echo $(stty)
reset() {
stty echo
echo $(stty)
exit
}
trap reset int
read -s
sleep 10
我希望 echo 选项被启用,但是在按下 ctrlc 之后它仍然被禁用,即使我有 运行 stty echo
(正如您在 reset
函数的输出中看到的那样)。
正如评论中的@KamilCuk read
保存配置并在进程存在时恢复配置。这导致在 read
被 运行ning 时完成的修改被丢弃。解决方案是在 运行 宁 read
之前恢复默认值并在 read
完成后重做它们。
stty -echo
echo $(stty)
reset() {
stty echo
echo $(stty)
exit
}
trap reset int
stty echo # This line has been added
read -s
echo read finished
stty -echo # This line has been added
sleep 10
@JonathanLeffler 也
It can be useful to use
old=$(stty -g)
to capture the current terminal settings, and then usestty "$old"
to reinstate those settings.
Using it allows you to reinstate the exact terminal settings when
stty -g
was invoked. This can be more reliable than undoing changes withstty -echo
etc.
我认为这是更合适的解决方案,因为 stty
可能 运行 默认处于无回显模式。