您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Linux:传送到Python(ncurses)脚本,stdin和termios

Linux:传送到Python(ncurses)脚本,stdin和termios

没有父流程的参与就无法做到这一点。幸运的是,有一种使用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

python 2022/1/1 18:44:02 有317人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶