Why do my keystrokes turn into crazy characters after I dump a bunch of binary data into my terminal?

喜你入骨 提交于 2019-12-13 12:24:22

问题


If I do something like:

$ cat /bin/ls

into my terminal, I understand why I see a bunch of binary data, representing the ls executable. But afterwards, when I get my prompt back, my own keystrokes look crazy. I type "a" and I get a weird diagonal line. I type "b" and I get a degree symbol.

Why does this happen?


回答1:


Because somewhere in your binary data were some control sequences that your terminal interpreted as requests to, for example, change the character set used to draw. You can restore everything to normal like so:

reset



回答2:


Just do a copy-paste:

echo -e '\017'

to your bash and characters will return to normal. If you don't run bash, try the following keystrokes:

<Ctrl-V><Ctrl-O><Enter>

and hopefully your terminal's status will return to normal when it complains that it can't find either a <Ctrl-V><Ctrl-O> or a <Ctrl-O> command to run.

<Ctrl-N>, or character 14 —when sent to your terminal— orders to switch to a special graphics mode, where letters and numbers are replaced with symbols. <Ctrl-O>, or character 15, restores things back to normal.




回答3:


The terminal will try to interpret the binary data thrown at it as control codes, and garble itself up in the process, so you need to sanitize your tty.

Run:

stty sane

And things should be back to normal. Even if the command looks garbled as you type it, the actual characters are being stored correctly, and when you press return the command will be invoked.

You can find more information about the stty command here.




回答4:


You're getting some control characters piped into the shell that are telling the shell to alter its behavior and print things differently.




回答5:


VT100 is pretty much the standard command set used for terminal windows, but there are a lot of extensions. Some control character set used, keyboard mapping, etc.

When you send a lot of binary characters to such a terminal, a lot of settings change. Some terminals have options to 'clear' the settings back to default, but in general they simply weren't made for binary data.

VT100 and its successors are what allow Linux to print in color text (such as colored ls listings) in a simple terminal program.

-Adam




回答6:


If you really must dump binary data to your terminal, you'd have much better luck if you pipe it to a pager like less, which will display it in a slightly more readable format. (You may also be interested in strings and od, both can be useful if you're fiddling around with binary files.)



来源:https://stackoverflow.com/questions/121282/why-do-my-keystrokes-turn-into-crazy-characters-after-i-dump-a-bunch-of-binary-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!