栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

最后,您可以通过先运行subshel​​l来解决命令行中的丑陋语法:

$ exec 3<&0  # spawn subshell$ echo "foo" | ./pipe.py  # works$ echo "bar" | ./pipe.py  # still works

如果有,那可以解决您的问题

bash



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/611567.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号