您如何通过管道将输入和输出传递给交互式shell? [英] How do you pipe input and output to and from an interactive shell?

查看:82
本文介绍了您如何通过管道将输入和输出传递给交互式shell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个使用户能够与IRB或Python之类的命令行交互式shell进行交互的应用程序.这意味着我需要将用户输入传递到外壳中,并将外壳的输出返回给用户.

I am trying to build an application that enables the user to interact with a command-line interactive shell, like IRB or Python. This means that I need to pipe user input into the shell and the shell's output back to the user.

我希望这与传递STDIN,STDOUT和STDERR一样容易,但是大多数外壳对STDIN输入的响应似乎与直接键盘输入不同.

I was hoping this was going to be as easy as piping STDIN, STDOUT, and STDERR, but most shells seem to respond differently to STDIN input as opposed to direct keyboard input.

例如,当我将STDIN传递到 python 时,会发生以下情况:

For example, here is what happens when I pipe STDIN into python:

$ python 1> py.out 2> py.err <<EOI
> print 'hello'
> hello
> print 'goodbye'
> EOI
$ cat py.out
hello
$ cat py.err
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'hello' is not defined

Python似乎将STDIN解释为脚本文件,并且它没有通过管道传递任何交互式界面,例如该行开头的">>>".它在第一行也失败,并显示错误,因为我们在输出文件中看不到再见".

It seems that Python is interpreting the STDIN as a script file, and it doesn't pipe any of the interactive interface, like the ">>>" at the beginning of the line. It also fails at the first line with an error, because we do not see "goodbye" in the outfile.

以下是 irb (交互式Ruby)发生的情况:

Here is what happens with irb (Interactive Ruby):

$ irb 1> irb.out 2> irb.err <<EOI
> puts 'hello'
> hello
> puts 'goodbye'
> EOI
$ cat irb.out
Switch to inspect mode.
puts 'hello'
hello
nil
hello
NameError: undefined local variable or method `hello' for main:Object
    from (irb):2
    from /path/to/irb:16:in `<main>'
puts 'goodbye'
goodbye
nil

$ cat irb.err

IRB的响应与Python的响应不同:即,即使发生错误,IRB仍继续执行命令.但是,它仍然缺少shell接口.

IRB responds differently than Python: namely, it continues executing commands even when there is an error. However, it still lacks the shell interface.

应用程序如何与交互式外壳环境进行交互?

How can an application interact with an interactive shell environment?

推荐答案

从技术上讲,您的第一个示例不是将输入传递给Python;而是将输入传递给Python.它来自文件-是的,对文件输入的处理方式有所不同.

Technically, your first sample is not piping the input to Python; it is coming from a file — and yes, file input is treated differently.

说服程序输入来自终端的方法是使用伪tty.有一个主端和一个从属端.您将把外壳(Python,Ruby)挂接到从属端,并让您的控制程序在主端进行读写.

The way to persuade a program that its input is coming from a terminal is using a pseudo-tty. There's a master side and a slave side; you'll hook the shell (Python, Ruby) to the slave side, and have your controlling program write to and read from the master side.

设置起来非常棘手.使用 expect 或其克隆之一来管理伪tty可能会更好.在其他相关问题中,请参见如何执行自动Unix输入?

That's fairly tricky to set up. You may do better using expect or one of its clones to manage the pseudo-tty. Amongst other related questions, see How to perform automated Unix input?

这篇关于您如何通过管道将输入和输出传递给交互式shell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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