-
-
Save daviddoria/fac54d3e8f2e01d21036 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
import select | |
process = subprocess.Popen("/bin/bash", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE); | |
process.stdin.write("export MyVar=\"Test\"\n") | |
process.stdin.write("echo ${MyVar}\n") | |
process.stdin.write("echo ${MyVar}\n") | |
try: | |
i,o,e = select.select([process.stdout], [], [], 5) # 5 second timeout | |
stdout = os.read(i[0].fileno(), 1024) | |
except IndexError: | |
# nothing was written to the pipe in 5 seconds | |
stdout = "" | |
print "stdout: " + stdout | |
process.stdin.write("echo $MyVar\n") | |
try: | |
i,o,e = select.select([process.stdout], [], [], 5) # 5 second timeout | |
stdout = os.read(i[0].fileno(), 1024) | |
except IndexError: | |
# nothing was written to the pipe in 5 seconds | |
stdout = "" | |
print "stdout: " + stdout | |
######## | |
Output: | |
stdout: Test | |
stdout: Test | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment