piping from stdin to a python code in a bash script
Question: I have a bash script, f, that contains python code. That python code reads
from standard input. I want to be able to call my bash script as follows:

    
    
    f input.txt > output.txt
    

In the example above, the python code will read from input.txt and will write
to output.txt.

I'm not sure how to do this. I know that if I wanted to just write to a file,
then my bash script would look like this

    
    
    #!/bin/bash
    python << EOPYTHON > output.txt
    #python code goes here
    EOPYTHON
    

I tried changing the second line in the code above to the following, but
without luck

    
    
    python << EOPYTHON $*
    

I'm not sure how else to go about doing this. Any suggestions?

**EDIT** I'll give a more concrete example. Consider the following bash
script, f

    
    
    #!/bin/bash
    python << EOPYTHON 
    import sys
    import fileinput
    for i in fileinput.input():
        sys.stdout.write(i + '\n')
    EOPYTHON
    

I want to run my code with the following command

    
    
    f input.txt > output.txt
    

How do I change my bash script so that it uses "input.txt" as the input
stream?


Answer: **Updated Answer**

If you absolutely must run the way you ask, you could do something like this:

    
    
    #!/bin/bash
    python -c 'import os
    for i in range(3):
       for j in range(3):
         print(i + j)
    '  < "$1"
    

**Original Answer**

Save your python code in a file called `script.py` and change your script `f`
to this:

    
    
    #!/bin/bash
    python script.py < "$1"
    

