python - 让 bash 从父进程读取命令提示 rune 本

标签 python python-3.x bash

我需要自动化一些 bash 脚本,其中涉及使用 y/n 回答 read 提示。

我尝试将 stdout/stderr/stdin 通过管道传输到 python 脚本。写入 stdin 可以工作,但从 stdout/stderr 读取提示文本由于某种原因无法工作? (我可以很好地阅读 bash 或子进程输出的所有其他内容。)

>>> from subprocess import Popen, PIPE
>>> proc = Popen(['bash','-c','read -r -p "Update system? [y/N] " response'],stdout=PIPE,stdin=PIPE,stderr=PIPE)
>>> proc.stdout.read(10) # <-- hangs, same with stderr, any length

我希望能够以某种方式阅读显示的提示“更新系统?[y/N]”,以便我可以决定传回什么答案。

最佳答案

关于python - 让 bash 从父进程读取命令提示 rune 本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51952165/

相关文章:

python - Python中是否有相当于//运算符的上限?

Python 3.5 aiohttp 即使在使用 async/await 时也会阻塞

python - 使用 Python Lambda 的(键,值)对

python - 从子进程调用的 bash 脚本的返回代码

linux - 用于查找、处理和重命名文件的 Bash 脚本?

python - 一定时间后 Shellscript : kill . py

python - 整个 .txt 文件都是字符串,但实际上不是。我如何按类型区分它们?

bash - 如果存储在局部变量中,函数调用的退出代码将被忽略

Python - 拆分包含 '\x' 的字符串

python - cx_Oracle : Error 933. ORA-00933 : “SQL command not properly ended” : SQL command error?