没有父流程的参与就无法做到这一点。幸运的是,有一种使用I / O重定向使bash参与其中的方法:
$ (echo "foo" | ./pipe.py) 3<&0
这将通过副本
foo传递到文件描述符3
pipe.py的子外壳中
stdin。现在,我们需要做的就是在python脚本中使用来自父进程的额外帮助(因为我们将继承fd
3):
#!/usr/bin/env pythonimport sys, osimport cursesoutput = 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()
最后,您可以通过先运行subshell来解决命令行中的丑陋语法:
$ exec 3<&0 # spawn subshell$ echo "foo" | ./pipe.py # works$ echo "bar" | ./pipe.py # still works
如果有,那可以解决您的问题
bash。



