4

Given the following python script....

$ cat readStdin.py 
#!/usr/bin/python

import sys

var = "".join(sys.stdin.readlines()).rstrip()
print var

... I get the follwing output:

$ echo hello  | python -i readStdin.py 
hello
>>> 
$

... in other words it does not hang in the python console, but goes back to bash. Does anyone out there know how to make it stay in the python console???

murungu
  • 2,090
  • 4
  • 21
  • 45
  • This isn't an artifact of `python -i` not working (it works fine). If you run your script without the shell pipeline it goes OK. The problem is that the python tries to read more data from `echo` but that data doesn't exist, so it gets `EOF` immediately and closes (as I understand it). Good question though ... – mgilson Oct 09 '12 at 15:34
  • Agreed, this hasn't anything to do with interactive mode. – Abhishek Mishra Oct 09 '12 at 15:41

1 Answers1

3

Consider this -

$ echo print  4*2 | python -i
Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
Type "help", "copyright", "credits" or "license" for more information.
>>> 8
>>> 
$

Echo produces print 4*2. Python even in interactive mode, considers this as input to be interpreted. Hence we see the 8 there. After this, the interpreter encounters an EOF, so it quits. Consider what you press to quit the interpreter - Ctrl+d or ^D. This is just another way to produce EOF on *nix.

Abhishek Mishra
  • 5,002
  • 8
  • 36
  • 38