简体   繁体   English

Linux Bash使用-n nchars读取和处理换行符

[英]Linux Bash 'read' and handling of newline with -n nchars

Following code: 以下代码:

read -n 3 -r -p "Enter up to 3 characters  -> : "
echo "string"

When typing two characters and pressing Return it outputs: 输入两个字符并按回车键时,输出:

Enter up to 3 characters  -> : 12
string

However, when entering 3 characters, without Return, it produces: 但是,当输入3个字符而没有Return时,它将产生:

Enter up to 3 characters  -> : 123string

I would like to see: 我想看看:

Enter up to 3 characters  -> : 123
string

Unfortunately, the newline character \\n is not part of the $REPLY variable, otherwise I could check it and output an echo if a new line was missing. 不幸的是,换行符\\ n不是$ REPLY变量的一部分,否则我可以检查它并在缺少新行的情况下输出回显。

The reason why I need this is to output an error message if there is invalid input, so using -N or -n 4 is not a solution. 我需要这样做的原因是,如果输入无效,则会输出一条错误消息,因此使用-N或-n 4并不是解决方案。

Any ideas? 有任何想法吗? Thanks! 谢谢!

Btw, I was already thinking of the following, which produces the output I want, but unfortunately, I already use "tput sc" for something else and can therefore not use it again. 顺便说一句,我已经在考虑以下内容,它将产生所需的输出,但是不幸的是,我已经将“ tput sc”用于其他内容,因此无法再次使用。

echo; tput sc; tput cuu1
read -n 3 -r -p "Enter up to 3 characters  -> : "
tput rc; echo "string"

You can check that the length of REPLY matches what you expect. 您可以检查REPLY的长度是否符合您的期望。 If it matches, then Enter was not pressed, so echo a blank line. 如果匹配,则未按Enter键,因此回显空白行。

len=3
read -n $len -r -p "Enter up to 3 characters  -> : "
(( ${#REPLY} == $len )) && echo
echo "string"

If the length is less than len , then Enter must have been pressed. 如果长度小于len ,则必须按下Enter键。

Or, if the user circumvented the input controls, then it probably doesn't really matter, does it? 或者,如果用户绕过了输入控件,那么可能并不重要,不是吗?

I've done a few more tests and while checking the length of $REPLY seemed to be the way to go at first, I finally ended up with a different solution. 我还做了一些测试,虽然最初检查$ REPLY的长度似乎是正确的方法,但最终还是得到了另一个解决方案。

I noticed that the backspace key when using read -n nchar was not deleting characters, but printed ^? 我注意到使用read -n nchar时的退格键不是删除字符,而是打印^?。 symbols instead. 符号代替。 I corrected the issue by adding readline support using the the -e parameter. 我通过使用-e参数添加readline支持来更正此问题。

-e :on interactive shells: use Bash's readline interface to read the data -e:在交互式shell上:使用Bash的readline界面读取数据

For example: 例如:

read -e -n 3 -r -p "Enter up to 3 characters  -> : "
or 
read -ern 3 -p Enter up to 3 characters  -> : "

This not only fixed the backspace or delete key, but also added the missing newline as described in the initial post. 这不仅修复了退格键或Delete键,而且还添加了缺少的换行符,如初始文章所述。 This makes checking the length of $REPLY no longer necessary, which would actually add an extra newline where none is needed. 这使得不再需要检查$ REPLY的长度,实际上会在不需要的地方添加额外的换行符。

Anyway, many thanks for your responses. 无论如何,非常感谢您的回复。 It did help me to continue with what I'm doing and last not least find the solution in the end, even though it was different than suggested. 它确实帮助我继续了我正在做的事情,并且最后还是最重要的是找到了解决方案,即使它与建议的有所不同。 Best regards! 最好的祝福!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM