繁体   English   中英

执行从标准输入到标准输出的命令,而不像 REPL 中的打印语句

[英]Execute commands from stdin to stdout without print statement like in REPL

使这项工作最简单的方法是什么:

> echo 1+1 | [run python in REPL and exit with capturing all statement values to stdout ]
2

是的,我们可以做echo "print(1+1)" | python echo "print(1+1)" | python但这需要写print 关键是要使 python 的行为类似于 PowerShell:

> echo 1+1 | powershell -c -
2

是的,我们可以做echo 1+1 | python -i echo 1+1 | python -i但 output 有一些噪音(python 版本等)。

以下不是推荐的做法,因为它使用不安全的eval 但是,在您所要求的范围内,您似乎可以控制流程中的所有步骤,它会做到的。

编写以下简单的 Python 程序( my_program.py ):

import sys

sys.stdout.write(str(eval(sys.stdin.read())))

然后,使用:

> echo 1+1 | python my_program.py
2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM