本文介绍了在逐字模式强制用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想任何读取用户输入键的逐字从Bash脚本,然后将其转储到十六进制。这就是:

I'm trying to any read user input keys in verbatim from Bash script, and then dump it into hex. That is:

read input
printf "%b" "$input" | xxd -p

如果用户preSS 2键 A 退格,我希望可以将输出为 617f ,不是空的。

If user press 2 keys , I would hope the output to be 617f, not empty.

我怎样才能做到这一点?

How can I achieve that?

推荐答案

这应该工作

#!/bin/bash

while true;do
    stty_state=$(stty -g)
    #Save stty to reset to default
    stty raw isig -echo
    #Set to raw and isig so nothing is interpretted and turn echo off so nothing is printed to screen.
    keypress=$(dd count=1 2>/dev/null)
    #Capture one character at a time
    #Redirect "errors" (from dd) output to dump
    keycode=$(printf "%s" "$keypress" | xxd -p)
    # Convert to hex
    stty "$stty_state"
    #Revert stty back
    printf "%s" "$keycode"
    #Print your key in hex
done

您可以把条件环路退出循环/程序,否则,你将需要使用 CTRL C `退出。

You can put a condition on the loop to exit the loop/program, otherwise you will need to use ` to exit.

这应该打印每一个关键preSS除了 CTRL C 和 CTRL 以Z

This should print every key press except for and .

这篇关于在逐字模式强制用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:24