在 Python 中运行 BASH 内置命令? [英] Run BASH built-in commands in Python?

查看:19
本文介绍了在 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')

及其许多变体.我想运行 historyfc -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()

感谢大家的意见.

这篇关于在 Python 中运行 BASH 内置命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆