周围的bash包装,控制STDIN和STDOUT [英] Wrapper around bash, control STDIN and STDOUT

查看:131
本文介绍了周围的bash包装,控制STDIN和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:


  1. 程序启动一个新的bash进程。

  2. 用户类型LS到我的程序。

  3. 程序发送这个命令的bash进程。

  4. 程序读取的bash的所有可用输出(包括提示),并显示给用户。

  5. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆