我无意中发现了这个奇怪的行为的巴什内置阅读。我有一个交互式脚本,它具有生成大量输出的潜力。所以很自然地,你会把| less
附加到上面。脚本仍然会要求您输入内容,但是它不会响应您输入的内容。
下面是一个小sample.sh:
#!/bin/bash
echo "Type:"
read -r input
echo "Typed: ${input}"
sample.sh | less
我注意到这不是管道的一般问题(例如|cat
works)。
任何线索都将不胜感激。
为我工作的解决方案:
#!/bin/bash
STTY_ORIG="$(stty -g)" # save stty settings
stty echo # enable echo
echo "Type:"
read -e -r input # use readline (backspace will not work otherwise)
echo "Typed: ${input}"
stty "${STTY_ORIG}" # restore stty settings
发布于 2018-12-08 12:59:44
一种对我有效且没有副作用的解决方案。基本上只是调整和恢复终端设置..。
#!/bin/bash
STTY_ORIG="$(stty -g)" # save stty settings
stty echo # enable echo
echo "Type:"
read -e -r input # use readline (backspace will not work otherwise)
echo "Typed: ${input}"
stty "${STTY_ORIG}" # restore stty settings
发布于 2018-11-21 06:27:44
实际上对我来说很管用。
同样的脚本
martus@makus-pc:/tmp/src$ dpkg -l | grep bash
ii bash 4.4-5 amd64 GNU Bourne Again SHell
martus@makus-pc:/tmp/src$ uname -a
Linux makus-pc 4.9.0-4-amd64 #1 SMP Debian 4.9.65-3+deb9u1 (2017-12-23) x86_64 GNU/Linux
编辑:脚本工作不需要管道少吗?在你按回车之前,更少不会显示任何键入的内容。
https://stackoverflow.com/questions/53414103
复制相似问题