巴什读取转义字符和普通字符 [英] Bash Read Escaped Characters and Normal Characters
问题描述
我要当的的关键是$ P $阅读在bash pssed 的,无论是方向键,数字,字母或标点符号,没有pressing进入。
这是我想出最接近的,除了当一个逃脱关键是pressed,它波及到下一个输入。此外,躲过键会读,但不回。
#!/斌/庆典读-r -n 1 -d $的'\\ n'-p'输入> 行
回声-en\\ n
回声$线
一个有点哈克/肮脏的方式,而应该为用户交互shell工作...
阅读-s -n1 -p输入> '剖面阅读-t 0.1 -N2 -s 2号线
行=$行$ 2号线
现在,就看你转换< ESC> A
字符串< UP>
或没有。
请注意:这很可能会失败,如果标准输入重定向(比如,从管/文件...)
I want to read in bash when any key is pressed, be it arrow key, number, letter, or punctuation, without pressing enter.
This is the closest that I have come up with, besides that when an escaped key is pressed, it spills over into the next input. Also, escaped keys will read but not echo.
#!/bin/bash
read -r -n 1 -d $'\n' -p 'input> ' line
echo -en "\n"
echo "$line"
A bit hacky/dirty way, but should work for user interactive shells...
read -n1 -s -p 'input> ' line; read -t 0.1 -n2 -s line2
line="$line$line2"
Now, it's up to you to convert <ESC>[A
to string <UP>
or not.
NOTE: It would most likely fail, if the stdin is redirected (say, from pipe/file...)
这篇关于巴什读取转义字符和普通字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!