有没有办法从 Python 运行 BASH 内置命令?
我试过了:
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
。
最佳答案
我终于找到了一个可行的解决方案。
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()
感谢大家的意见。
关于python - 在 Python 中运行 BASH 内置命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5460923/