![](/img/trans.png)
[英]In a bash script, how do I provide values contained in a text file to the switch of a program execution?
[英]How do i stop text showing up when user types during execution of bash script?
你可以通过sleep 10
和打字来了解我遇到了什么问题。
我将如何停止显示该打字?
要关闭键盘回声,您可以使用stty -echo
。 要重新打开它,您可以执行stty echo
。 为了安全起见,您将保存 stty state 并在最后恢复它; 但是,如果您正在读取密码,bash 有一个内置的-s
选项可以读取。
总之:
stty -echo
# keyboard input is not echoed
stty echo
# keyboard input is echoed
更完整:
state=$(stty -g)
stty -echo
# keyboard is not echoed
stty "$state"
# state is restored.
更好的是,如果您只是要求输入密码并使用 bash:
read -s password
# password is now in the variable $password; no stty shenanigans
你可以这样做:
#!/usr/bin/env bash
exit_trap ()
{
# Purge pending typed data if any
# While there is data to read
while read -rt0
do IFS= read -r # read the data
done < /dev/tty # from the tty terminal
# Restore terminal echo
stty echo
}
# If running from a terminal
if [ -t 1 ]
then
# Program exit trap to cleanup and restore terminal echo on
trap 'exit_trap' EXIT
# Disable terminal echo
stty -echo
fi
echo "going to sleep 5"
sleep 5
echo "finished sleeping"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.