在 Python 中运行 BASH 内置命令?
问题描述
有没有办法从 Python 运行 BASH 内置命令?
Is there a way to run the BASH built-in commands from Python?
我试过了:
subprocess.Popen(['bash','history'],shell=True, stdout=PIPE)
subprocess.Popen('history', shell=True, executable = "/bin/bash", stdout=subprocess.PIPE)
os.system('history')
及其许多变体.我想运行 history
或 fc -ln
.
and many variations thereof. I would like to run history
or fc -ln
.
解决方案
我终于找到了一个可行的解决方案.
I finally found a solution that works.
from subprocess import Popen, PIPE, STDOUT
shell_command = 'bash -i -c "history -r; history"'
event = Popen(shell_command, shell=True, stdin=PIPE, stdout=PIPE,
stderr=STDOUT)
output = event.communicate()
感谢大家的意见.
相关文章