运行Python和STDIN从猛砸

问题描述:

我有一个bash代码(Mybash1.sh),我的结果需要通过 另一个bash的代码(Mybash2.sh)包含的Python运行Python和STDIN从猛砸

这里是代码。 Mybash1.sh

#! /bin/bash 
# Mybash1.sh 
cut -f1,3 input_file.txt | sort | ./Mybash2.sh 

Mybash2.sh是这样的:

#! /bin/bash 
#Mybash2.sh 
python mycode.py foo.txt <("[email protected]") > output.txt 
# do something for output.txt 

我的问题是, “output.txt的” 在Mybash2.sh不含结果。 在Mybash2.sh中有没有正确的方法来执行Python?

请注意,mycode.py将工作,如果我运行它在Mybash1.sh给出的中间临时 文件。但我想避免使用它,因为在Mybash1.sh之内的很多情况下我都会拨打Mybash2.sh

mycode.py摘录如下:

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() 

在myscript1.sh

,你传递一个管道myscript2.sh,因此,它的某种STDIN为myscript2.sh。您应该从myscript2.sh读取STDIN,而不是输入参数。例如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 

最后,为什么所有这些依赖?你不能在Python中做任何事吗?

在python中,你想拥有file2 = sys.stdin

然后:

#! /bin/bash 
#Mybash2.sh 
python mycode.py foo.txt > output.txt 

编辑:我刚刚看到的FileInput文档,似乎如果您对供应“ - ”到fileinput.input(),它会读取标准输入,所以无任何变化在你的Python中,这应该工作:

#! /bin/bash 
#Mybash2.sh 
python mycode.py foo.txt - > output.txt