我想在python程序中运行以下linuxbash命令行。

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!?

最佳答案

这里有一个纯python解决方案,我认为它与您的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)

10-06 01:19