如何使 python 脚本“可通过管道传输”在bash中?
我写了一个脚本,我希望它可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看看这个简单的
echo.py
:running:
output:
debug_output.txt内容:
See this simple
echo.py
:running:
output:
debug_output.txt content:
我将用一个使用 fileinput< 的 grep 示例来补充其他答案/a> 实现 UNIX 工具的典型行为: 1) 如果没有指定参数,则从 stdin 读取数据; 2) 可以指定多个文件作为参数; 3) 单个参数
-
表示标准输入。例子:
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.Example:
在 Python 脚本中,您只需 从
stdin
读取。In your Python script you simply read from
stdin
.从标准输入读取的所有内容都是“可通过管道传输的”。 Pipe 只是将前一个程序的标准输出重定向到后者。
Everything that reads from stdin is "pipeable". Pipe simply redirects stdout of former program to the latter.