Skip to content

Instantly share code, notes, and snippets.

@daviddoria
Created April 17, 2014 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daviddoria/fac54d3e8f2e01d21036 to your computer and use it in GitHub Desktop.
Save daviddoria/fac54d3e8f2e01d21036 to your computer and use it in GitHub Desktop.
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