如何使 python 脚本“可通过管道传输”在bash中?

发布于 2024-10-07 20:14:03 字数 117 浏览 0 评论 0原文

我写了一个脚本,我希望它可以在 bash 中通过管道。比如:

echo "1stArg" | myscript.py

这可能吗?如何?

I wrote a script and I want it to be pipeable in bash. Something like:

echo "1stArg" | myscript.py

Is it possible? How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感情旳空白 2024-10-14 20:14:03

看看这个简单的echo.py

import sys

if __name__ == "__main__":
    for line in sys.stdin:
        sys.stderr.write("DEBUG: got line: " + line)
        sys.stdout.write(line)

running:

ls | python echo.py 2>debug_output.txt | sort

output:

echo.py
test.py
test.sh

debug_output.txt内容:

DEBUG: got line: echo.py
DEBUG: got line: test.py
DEBUG: got line: test.sh

See this simple echo.py:

import sys

if __name__ == "__main__":
    for line in sys.stdin:
        sys.stderr.write("DEBUG: got line: " + line)
        sys.stdout.write(line)

running:

ls | python echo.py 2>debug_output.txt | sort

output:

echo.py
test.py
test.sh

debug_output.txt content:

DEBUG: got line: echo.py
DEBUG: got line: test.py
DEBUG: got line: test.sh
漫漫岁月 2024-10-14 20:14:03

我将用一个使用 fileinput< 的 grep 示例来补充其他答案/a> 实现 UNIX 工具的典型行为: 1) 如果没有指定参数,则从 stdin 读取数据; 2) 可以指定多个文件作为参数; 3) 单个参数 - 表示标准输入。

import fileinput
import re
import sys

def grep(lines, regexp):
    return (line for line in lines if regexp.search(line))

def main(args):
    if len(args) < 1:
        print("Usage: grep.py PATTERN [FILE...]", file=sys.stderr)
        return 2 

    regexp = re.compile(args[0])
    input_lines = fileinput.input(args[1:])

    for output_line in grep(input_lines, regexp):
        sys.stdout.write(output_line)

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

例子:

$ seq 1 20 | python grep.py "4"
4
14

I'll complement the other answers with a grep example that uses fileinput to implement the typical behaviour of UNIX tools: 1) if no arguments are specified, it reads data from stdin; 2) many files can be specified as arguments; 3) a single argument of - means stdin.

import fileinput
import re
import sys

def grep(lines, regexp):
    return (line for line in lines if regexp.search(line))

def main(args):
    if len(args) < 1:
        print("Usage: grep.py PATTERN [FILE...]", file=sys.stderr)
        return 2 

    regexp = re.compile(args[0])
    input_lines = fileinput.input(args[1:])

    for output_line in grep(input_lines, regexp):
        sys.stdout.write(output_line)

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

Example:

$ seq 1 20 | python grep.py "4"
4
14
二智少女猫性小仙女 2024-10-14 20:14:03

在 Python 脚本中,您只需 stdin 读取

In your Python script you simply read from stdin.

千仐 2024-10-14 20:14:03

从标准输入读取的所有内容都是“可通过管道传输的”。 Pipe 只是将前一个程序的标准输出重定向到后者。

Everything that reads from stdin is "pipeable". Pipe simply redirects stdout of former program to the latter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文