如何在python中运行多行bash命令? [英] How to run multi-line bash commands inside python?
本文介绍了如何在python中运行多行bash命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在python程序中运行以下linux bash
命令行.
I want to run the following lines of linux bash
commands inside a python program.
tail /var/log/omxlog | stdbuf -o0 grep player_new | while read i
do
Values=$(omxd S | awk -F/ '{print $NF}')
x1="${Values}"
x7="${x1##*_}"
x8="${x7%.*}"
echo ${x8}
done
我知道对于单行命令,我们可以使用以下语法:
I know that for a single-line command, we can use the following syntax:
subprocess.call(['my','command'])
但是,如果多行中有多个命令,我该如何使用 subprocess.call
!?
But, how can I use subprocess.call
if there are several commands in multiple lines !?
推荐答案
这是我认为 与您的 bash
相同的纯python解决方案:
Here is a pure python solution that I think does the same as your bash
:
logname = '/var/log/omxlog'
with open(logname, 'rb') as f:
# not sure why you only want the last 10 lines, but here you go
lines = f.readlines()[-10:]
for line in lines:
if 'player_new' in line:
omxd = os.popen('omxd S').read()
after_ = omxd[line.rfind('_')+1:]
before_dot = after_[:after_.rfind('.')]
print(before_dot)
这篇关于如何在python中运行多行bash命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文