Running Python With STDIN From Bash
I have a bash code (Mybash1.sh
) where the result I need to pass
to another bash code (Mybash2.sh
) that contain Python
Here are the codes. Mybash1.sh
#! /bin/bash
# Mybash1.sh
cut -f1,3 input_file.txt | sort | ./Mybash2.sh
Mybash2.sh is this:
#! /bin/bash
#Mybash2.sh
python mycode.py foo.txt <("$@") > output.txt
# do something for output.txt
The problem I have is that "output.txt" in Mybash2.sh
contain no result.
Is there a correct way to execute Python in Mybash2.sh
?
Note that mycode.py
will work if I run it on an intermediate temporary
file given from Mybash1.sh
. But I wanted to avoid using that, since I will call Mybash2.sh
in many instances within Mybash1.sh
.
Snippet of mycode.py
looks like this:
if __name__ == "__main__":
开发者_运维问答 import sys, os, fileinput
progName = os.path.basename(sys.argv[0])
if len(sys.argv) != 3:
sys.exit('Usage: ' + progName + ' file1 file2')
file1 = fileinput.input(sys.argv[1])
file2 = fileinput.input(sys.argv[2])
# do something for file1 and file2
file1.close()
file2.close()
In python, you want to have file2 = sys.stdin
.
And then:
#! /bin/bash
#Mybash2.sh
python mycode.py foo.txt > output.txt
EDIT: I've just seen the fileinput docs and it seems that if you supply '-' to fileinput.input(), it will read stdin, so without any changes in your Python, this should work:
#! /bin/bash
#Mybash2.sh
python mycode.py foo.txt - > output.txt
in your myscript1.sh, you are passing a pipeline to myscript2.sh, therefore, its some sort of STDIN for myscript2.sh. You should read that STDIN from myscript2.sh, not taking in input arguments. eg myscript2.sh
#!/bin/bash
while read myinput
do
echo "myinput is $myinput"
# assuming you are passing each input line from the cut command into Python
python mycode.py foo.txt $myinput > output.txt
done
Lastly, why all these dependencies? can't you do everything in Python, or shell??
精彩评论