当前位置:网站首页>shell小技巧(一百三十四)简单的键盘输入记录器
shell小技巧(一百三十四)简单的键盘输入记录器
2022-06-25 06:39:00 【乐大师】
实现效果:
运行脚本,提示输入4个字符。
输入过程中,未达到4个字符之前五显示。
输入第4个字符后,自动显示输入的4个字符。
#!/bin/bash
keypresses=4
old_tty_setting=$(stty -g)
echo "Presss $keypresses keys"
stty -icanon -echo
keys=$(dd bs=1 count=$keypresses 2> /dev/null)
stty "$old_tty_setting"
echo "You pressed the \"$keys\" keys."
exit 0

代码简要说明:
stty -g输出stty当前设置,存储在变量old_tty_setting中
stty -icanon -echo 将不显示标准输入内容于终端屏幕
dd bs=1 count=$keypresses 2> /dev/null 这里没有设置if,会默认使用标准输入。配合上面的stty设置,实现了键盘输入(即标准输入)内容被dd命令获得,存储于变量keys
stty "$old_tty_setting" 恢复原来的设置
边栏推荐
- Practice of hierarchical management based on kubesphere
- lotus v1.16.0-rc2 Calibration-net
- Common functions of OrCAD schematic
- Zhugeliang vs pangtong, taking distributed Paxos
- 破万,我用了六年!
- keepalived監控進程,自動重啟服務進程
- Why is true == "true" true in R- Why TRUE == “TRUE” is TRUE in R?
- SQL solve select basic statement
- 【UVM入门 ===> Episode_9 】~ 寄存器模型、寄存器模型的集成、寄存器模型的常规方法、寄存器模型的应用场景
- 14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
猜你喜欢

太上老君的炼丹炉之分布式 Quorum NWR

Debug through yalc before releasing NPM package

【批處理DOS-CMD命令-匯總和小結】-外部命令-cmd下載命令、抓包命令(wget)

几款不错的天气插件

Hanxin's trick: consistent hashing

【批处理DOS-CMD命令-汇总和小结】-应用程序启动和调用、服务和进程操作命令(start、call、)

Several schemes of traffic exposure in kubernetes cluster

LabVIEW jump to web page

稳压二极管的原理,它有什么作用?

5g private network market is in full swing, and it is crucial to solve deployment difficulties in 2022
随机推荐
1W words | 40 pictures | hard core es actual combat
Explain distributed raft with dynamic diagram
Kube scheduler source code analysis (1) - initialization and startup analysis
线程状态变化涉及哪些常用 API
alphassl通配符证书送一个月
Alphassl wildcard certificate for one month
Why "New Year's Eve", the original memory burst!
Large funds support ecological construction, and Plato farm builds a real meta universe with Dao as its governance
有了 MySQL 为什么要用 NoSQL?
lotus windowPoSt 手动触发时空证明计算
313. Binary sum
Enter an integer with any number of bits, and output the sum of each bit of the number. For example: 1234 – > 10
Google extender address
Cocos学习日记3——api获取节点、组件
LabVIEW jump to web page
1W字|40 图|硬核 ES 实战
[C language] add separator to string
分布式锁中的王者方案 - Redisson
Is it possible to use Jasmine's toHaveBeenCalledWith matcher with a regular expression?
[batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)