【问题标题】:Prevent typed characters from being displayed (like disabling "echo" attribute in termios)防止显示键入的字符(例如在 termios 中禁用“echo”属性)
【发布时间】:2012-06-14 18:25:07
【问题描述】:

我正在编写一个 bash 脚本,在该脚本中我从输入中读取单个字符。我这样做是使用read -n 1 -s-n 1 是只读单个字符; -s 是“静默”模式,输入的字符将不可见。

问题是,当当前执行的命令不是 read 时(当 bash 脚本中的一些其他命令正在执行时),字符会显示在终端中。

这是终端中程序的正常行为。要禁用此功能,通常会禁用回显模式,例如使用 termios 库。

如何在 bash 脚本中实现这一点?

我更喜欢纯 bash / Unix 命令的解决方案(没有其他脚本语言,如 python、perl 等)。

【问题讨论】:

    标签: bash scripting terminal termios


    【解决方案1】:
    stty -echo
    # Anything they type won't output here
    stty echo
    # Now it will
    

    【讨论】:

      猜你喜欢
      • 2013-04-29
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多