How to run multi-line bash commands inside python?

后端 未结 2 743
悲哀的现实
悲哀的现实 2021-01-25 03:59

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
d         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-25 04:20

    quote https://mail.python.org/pipermail/tutor/2013-January/093474.html:
    use subprocess.check_output(shell_command, shell=True)

    import subprocess
    cmd = '''
    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.check_output(cmd, shell=True)
    

    I have try some other examples and it works.

提交回复
热议问题