周围的bash包装,控制STDIN和STDOUT [英] Wrapper around bash, control STDIN and STDOUT
问题描述
我想谈一个互动的bash进程。
I would like to talk to a interactive bash process.
下面是一个例子,让你知道我想要什么archieve:
Here is an example, so you know what I want to archieve:
- 程序启动一个新的bash进程。
- 用户类型LS到我的程序。
- 程序发送这个命令的bash进程。
- 程序读取的bash的所有可用输出(包括提示),并显示给用户。
- GOTO 1
正如您可以猜到,有很大的空间漂亮的操作在这里和那里...; - )
As you can guess, there is much room for nifty manipulations here and there... ;-)
这将是美好的,如果这还曾任职于子进程(由bash进程启动),并基于光标的计划。
It would be wonderful if this also worked for subprocesses (started by the bash process) and curses-based programs.
我想在Ruby中实现这一功能,并且已经与IO.popen尝试,但奇怪的事情发生。也欢迎您做到这一点其他语言。
I would like to implement this functionality in Ruby, and already have experimented with IO.popen, but strange things happen. You are also welcome to do this in other languages.
推荐答案
好吧,我已经找到了解决办法。这项工作pretty很好,你甚至可以在它里面启动Vim: - )
Ok, I've found a solution. This work pretty nicely, you can even start vim inside it :-)
require "pty"
system("stty raw -echo")
PTY.spawn("bash -i") do |pin, pout|
Thread.new do
loop do
pout.print STDIN.getc.chr
end
end
loop do
print pin.sysread(512)
STDOUT.flush
end
end
这将执行以下操作:
- 启用字符明智输入(仅限于UNIXoids,只怕)
- 创建一个伪TTY,内部启动一个交互式bash命令
- 从标准输入每个字符转发到庆典
- 打印每个输出返回给用户
这篇关于周围的bash包装,控制STDIN和STDOUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!