没有父流程的参与就无法做到这一点。幸运的是,有一种使用I / O重定向使bash参与其中的方法:
$ (echo "foo" | ./pipe.py) 3<&0
这将通过副本foo
传递到文件描述符3pipe.py
的子外壳中stdin
。现在,我们需要做的就是在python脚本中使用来自父进程的额外帮助(因为我们将继承fd 3):
#!/usr/bin/env python
import sys, os
import curses
output = sys.stdin.readline(100)
# We're finished with stdin. Duplicate inherited fd 3,
# which contains a duplicate of the parent process' stdin,
# into our stdin, at the OS level (assigning os.fdopen(3)
# to sys.stdin or sys.__stdin__ does not work).
os.dup2(3, 0)
# Now curses can initialize.
screen = curses.initscr()
screen.border(0)
screen.addstr(12, 25, output)
screen.refresh()
screen.getch()
curses.endwin()
最后,您可以通过先运行subshel??l来解决命令行中的丑陋语法:
$ exec 3<&0 # spawn subshell
$ echo "foo" | ./pipe.py # works
$ echo "bar" | ./pipe.py # still works
如果有,那可以解决您的问题bash
。