如何在python中运行多行bash命令?

我想在python程序中运行以下linux bash命令行.

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

我知道对于单行命令,我们可以使用以下语法:

subprocess.call(['my','command'])

但是,如果在多行中有多个命令,该如何使用subprocess.call呢?

解决方法:

这是我认为与您的bash相同的纯python解决方案:

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子进程模块


下一篇:Python子进程模块比命令慢得多(不建议使用)