从标准输入管道到 bash 脚本中的 python 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22629240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:00:23  来源:igfitidea点击:

piping from stdin to a python code in a bash script

pythonbash

提问by user2699231

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:

我有一个包含 python 代码的 bash 脚本 f。该 python 代码从标准输入读取。我希望能够按如下方式调用我的 bash 脚本:

f input.txt > output.txt

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

在上面的示例中,python 代码将从 input.txt 读取并写入 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

我不知道该怎么做。我知道如果我只想写入文件,那么我的 bash 脚本将如下所示

#!/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?

我不知道还有什么办法去做这件事。有什么建议?

EDITI'll give a more concrete example. Consider the following bash script, f

编辑我会给出一个更具体的例子。考虑以下 bash 脚本,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?

如何更改我的 bash 脚本,使其使用“input.txt”作为输入流?

采纳答案by Mark Setchell

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)
'  < ""

Original Answer

原答案

Save your python code in a file called script.pyand change your script fto this:

将您的 Python 代码保存在一个名为的文件中script.py,并将您的脚本更改f为:

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

回答by Reishin

As no one mentioned this, here is what author requested. The magic is to pass "-" as argument to cpython (instruction to read source code from stdin):

由于没有人提到这一点,这是作者要求的。神奇的是将“-”作为参数传递给 cpython(从标准输入读取源代码的指令):

With output to file:

输出到文件:

python - << EOF > out.txt
print("hello")
EOF

Execution sample:

执行示例:

# python - << EOF
> print("hello")
> EOF
hello

As data can't be passed via stdin anymore, here is another trick:

由于数据不能再通过标准输入传递,这是另一个技巧:

data=`cat input.txt`

python - <<EOF

data="""${data}"""
print(data)

EOF

回答by jfs

-coption from @Mark Setchell's answershould work.

-c@Mark Setchell 的答案中的选项应该有效。

Here's an alternative where you embed bashin Python script:

这是您嵌入bashPython 脚本的替代方法:

#!/usr/bin/env python
import sys
import fileinput
from subprocess import call

# shell command before the python code
rc = call(r"""
some bash-specific commands here
...
""", shell=True, executable='/bin/bash')

for line in fileinput.input():
    sys.stdout.write(line) #NOTE: `line` already has a newline

# shell command after the python code
rc = call(r"""
some /bin/sh commands here
...
""", shell=True)

回答by Reinstate Monica Please

You can just check it against the file descriptor list for the process, i.e. on a proc filesystem you can print the redirection location for stdout with

您可以根据进程的文件描述符列表检查它,即在 proc 文件系统上,您可以打印 stdout 的重定向位置

readlink /proc/$$/fd/1

For example

例如

> cat test.sh
#!/bin/bash
readlink /proc/$$/fd/1
> ./test.sh
/dev/pts/3
> ./test.sh > out.txt
> cat out.txt 
/home/out.txt