简体   繁体   English

如何将此Shell脚本转换为python?

[英]How can I translate this shell script to python?

I'd like to translate the shell code below to python code. 我想将下面的shell代码转换为python代码。
subprocess.Popen is used for doing it? subprocess.Popen是用来做的吗?
I've tried it, but I don't understand how to give (1),(2) commands. 我已经尝试过了,但是我不明白如何发出(1),(2)命令。

sh> cat a.sh
dc << EOF
  20 5 / p    --(1)
  10 4 * p    --(2)
EOF
sh> sh a.sh
4
40

dc is just an example program for asking a question. dc只是一个询问问题的示例程序。
This can be replaced with any program which acts like this. 可以用任何像这样的程序来代替它。
python version is 2.7.6. python版本是2.7.6。 (Maybe sh module is not included in this version so it seems I can't use sh.Command) (也许sh模块未包含在此版本中,所以看来我不能使用sh.Command)

Thanks. 谢谢。

>>> from subprocess import Popen,PIPE,STDOUT
>>> p = Popen(['dc'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
>>> output = p.communicate(input=b'20 5 / p\n10 4 * p')[0]
>>> print(output.decode())
4
40

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

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