dua*_*ign 18 python bash command subprocess
有没有办法从Python运行BASH内置命令?
我试过了:
subprocess.Popen(['bash','history'],shell=True, stdout=PIPE)
subprocess.Popen('history', shell=True, executable = "/bin/bash", stdout=subprocess.PIPE)
os.system('history')
Run Code Online (Sandbox Code Playgroud)
及其许多变化.我想跑history
或fc -ln
.
dua*_*ign 18
我终于找到了一个有效的解决方案.
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()
Run Code Online (Sandbox Code Playgroud)
谢谢大家的意见.
les*_*ana 14
subprocess.Popen(["bash", "-c", "type type"])
Run Code Online (Sandbox Code Playgroud)
这会调用bash并告诉bash运行字符串type type
,该字符串type
在参数上运行内置命令type
.
输出: type is a shell builtin
后面的部分-c
必须是一个字符串.这不起作用:["bash", "-c", "type", "type"]
归档时间: |
|
查看次数: |
7321 次 |
最近记录: |