Reputation: 2387
I need to automate a few bash scripts which involves answering to read
prompts with y/n
.
I tried to pipe stdout
/stderr
/stdin
to a python script. Writing to stdin
works but reading the prompt text from stdout
/stderr
doesn't for some reason? (I can read everything else that bash or sub-processes output fine.)
>>> 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
I was expecting I would be able to read displayed prompt "Update system? [y/N] "
somehow so I can decide what answer to pass back.
Upvotes: 1
Views: 118
Reputation: 1802
This is what expect
is good at:
https://likegeeks.com/expect-command/
Expect and bash https://unix.stackexchange.com/questions/351446/bash-and-expect-in-the-same-script
Upvotes: 1