read -p command in linux script is not displayed correctly by subprocess in python -


i want read output line line

below bash code (meta.sh)

#!/bin/bash echo "hello!" read -p "continue?(y/n)" [ "$reply" == "y" ] || exit echo "lol" 

below subprocess code (test.py)

import subprocess def create_subprocess(command):     proc = subprocess.popen(        command,        shell=true,        stdout=subprocess.pipe,        stderr=subprocess.stdout,        bufsize=1       )    return proc command = "/root/desktop/meta.sh" proc = create_subprocess(command) while true:     line = proc.stdout.readline()     if not line: break     print line.strip() 

now when run "python test.py" shows

hello!

now when press y , press enter shows

continue?(y/n)

lol

what ideally should show this

hello!

continue?(y/n)

now when press y , press enter should show

lol

as mentioned in comment, problem python's readline waiting until gets either end of line or end of file, neither of produced read -p command (note type onto same line, because there not line ending on one). means readline doesn't give until after lol printed. can around reading 1 char @ time though, so:

while true:     cur = proc.stdout.read(1)     if(not cur):         break     sys.stdout.write(cur)     sys.stdout.flush() 

though you'll have import sys well

this display each character proc.stdout it's read , not wait newlines.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -