通过标准输入的箭头键

发布于 2024-08-12 18:53:13 字数 203 浏览 4 评论 0原文

我正在尝试通过标准输入将箭头键发送到 bash:

cat | /bin/bash

然后我输入“echo hi”=> “hi”出现在控制台上(当然没有引号) 然后我按向上箭头键=> ^[[出现未找到命令

是否可以通过标准输入向程序发送箭头键?

我问的原因是:我想从另一个程序控制bash。我想将箭头键发送到 bash

I'm trying to send an arrow key via the stdin to the bash:

cat | /bin/bash

then i am typing "echo hi" => "hi" appears on the console (of course without the quotes)
then i press the arrow key up => ^[[A command not found appears

Is it possible to send an arrow key to an program via the stdin ?

The reason why i am asking is: I want to control the bash from another programm. I would like to send arrow keys to the bash

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅此而已 2024-08-19 18:53:13

您真正应该做的是创建一个伪 tty 设备(使用 openpty() 或类似的),在该 PTY 上启动 bash,然后通过该 PTY 发送您的击键设备。请参阅 GNU C 中的“伪终端”部分图书馆手册。

What you really should be doing is create a pseudo-tty device (using openpty() or similar), start bash on that PTY, and send your key strokes through that PTY device. See the section on “Pseudo-Terminals” in the GNU C Library Manual.

狂之美人 2024-08-19 18:53:13

尝试使用 -i 开关启动 bash。

Try starting bash with the -i switch.

一萌ing 2024-08-19 18:53:13

不要使用cat。使用带有 -e 选项的 Bash 内置 read 命令来启用 readline 支持。

# version 1
while IFS="" read -r -e -d 
\n' line; do printf '%s\n' "$line"; done | /bin/bash

# version 2
#set -o pipefail
# kill 0: kill process group
(
while IFS="" read -r -e -d 
\n' line; do 
   #trap 'trap - EXIT HUP INT QUIT PIPE TERM ERR; kill $PPID' EXIT HUP INT QUIT PIPE TERM ERR 
   trap 'trap - EXIT HUP INT QUIT PIPE TERM ERR; kill 0' EXIT HUP INT QUIT PIPE TERM ERR 
   printf '%s\n' "$line" >> ~/.bash_history
   history -n
   printf '%s\n' "$line" 
done 
) | (trap 'trap - EXIT HUP INT QUIT PIPE TERM ERR; kill 0' EXIT HUP INT QUIT PIPE TERM ERR; /bin/bash)
#) | (trap 'trap - EXIT HUP INT QUIT PIPE TERM ERR; kill $PPID' EXIT HUP INT QUIT PIPE TERM ERR; /bin/bash)

Don't use cat. Use the Bash builtin read command with the -e option to enable readline support.

# version 1
while IFS="" read -r -e -d 
\n' line; do printf '%s\n' "$line"; done | /bin/bash

# version 2
#set -o pipefail
# kill 0: kill process group
(
while IFS="" read -r -e -d 
\n' line; do 
   #trap 'trap - EXIT HUP INT QUIT PIPE TERM ERR; kill $PPID' EXIT HUP INT QUIT PIPE TERM ERR 
   trap 'trap - EXIT HUP INT QUIT PIPE TERM ERR; kill 0' EXIT HUP INT QUIT PIPE TERM ERR 
   printf '%s\n' "$line" >> ~/.bash_history
   history -n
   printf '%s\n' "$line" 
done 
) | (trap 'trap - EXIT HUP INT QUIT PIPE TERM ERR; kill 0' EXIT HUP INT QUIT PIPE TERM ERR; /bin/bash)
#) | (trap 'trap - EXIT HUP INT QUIT PIPE TERM ERR; kill $PPID' EXIT HUP INT QUIT PIPE TERM ERR; /bin/bash)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文