Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过管道将输入和输出传递到交互式shell以及从交互式shell输出?_Shell_Stdout_Stdin_Interactive_Irb - Fatal编程技术网

如何通过管道将输入和输出传递到交互式shell以及从交互式shell输出?

如何通过管道将输入和输出传递到交互式shell以及从交互式shell输出?,shell,stdout,stdin,interactive,irb,Shell,Stdout,Stdin,Interactive,Irb,我试图构建一个应用程序,使用户能够与命令行交互式shell(如IRB或Python)交互。这意味着我需要将用户输入导入shell,并将shell的输出返回给用户 我希望这会像管道STDIN、STDOUT和STDERR一样简单,但大多数shell对STDIN输入的响应似乎与直接键盘输入不同 例如,下面是我将STDIN导入python时发生的情况: $ python 1> py.out 2> py.err <<EOI > print 'hello' > hello

我试图构建一个应用程序,使用户能够与命令行交互式shell(如IRB或Python)交互。这意味着我需要将用户输入导入shell,并将shell的输出返回给用户

我希望这会像管道STDIN、STDOUT和STDERR一样简单,但大多数shell对STDIN输入的响应似乎与直接键盘输入不同

例如,下面是我将STDIN导入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
IRB的响应与Python不同:即,即使出现错误,它也会继续执行命令。但是,它仍然缺少shell接口


应用程序如何与交互式shell环境交互?

从技术上讲,您的第一个示例不是将输入管道化到Python;它来自一个文件——是的,文件输入的处理方式是不同的

说服程序其输入来自终端的方法是使用伪tty。有一个主端和一个从端;您将把shell(Python、Ruby)挂接到从端,并让您的控制程序从主端写入和读取


这是相当棘手的设置。使用
expect
或它的一个克隆来管理伪tty可能会做得更好。除其他相关问题外,请参见

谢谢您提供的信息。我不知道应用程序能够判断它们的STDIN是来自键盘(tty)还是来自管道/文件。在我的例子中,我很幸运,我使用的shell有一个选项可以强制它进入“交互模式”,这使得它跳过tty测试。如果您使用的是shell XYZ,快速搜索“XYZ强制交互模式”将告诉您该选项是否已经存在。
$ 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