5

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'])

But, how can I use subprocess.call if there are several commands in multiple lines !?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Omid1989
  • 419
  • 3
  • 8
  • 17
  • 2
    I don't know that this is a proper solution, but in bash you can substitute multiple lines with semi-colons. For example, `tail /var/log/omxlog | stdbuf -o0 grep plater_new | while read i; do Values=$(omxd S | awk -F/ '{print $NF}'); x1="${Values}";...` and so on. It certainly isn't very readable, but it should work. Is there any reason you couldn't have a bash script to run instead? – Guest Feb 18 '17 at 07:00
  • Why can't you put it in a script instead? – Inian Feb 18 '17 at 07:01
  • 1
    there is some good stuff in this post about using subprocess.pipe http://stackoverflow.com/a/13332300/1113788 another option might be to look at the python fabric library that has various options for executing local and remote code – davidejones Feb 18 '17 at 07:03
  • I don'w want to call external scripts inside python, because of accessing I/O SPI peripherals inside python. – Omid1989 Feb 18 '17 at 07:04
  • 1
    Why not just read `/var/log/omxlog` and execute `omxd` directly in python? `bash` seems to be unnecessary here. – Stephen Rauch Feb 18 '17 at 07:16
  • @StephenRauch, I'd be glad if you provide the equivalent python code for my bash script :) – Omid1989 Feb 18 '17 at 07:18
  • @davidejones, As you mentioned about python fabric, it doesn't have option to run a logical flow of shell commands over single session. But putting such commands as oneline code and able to run it which will produce unexpected results. Otherwise, we can run shell commands individually with run() or sudo() methods might help you to achieve simple operations on shell. – S.K. Venkat Mar 21 '18 at 14:12

2 Answers2

13

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.

wt.cc
  • 313
  • 1
  • 9
  • Is there any similar way to execute such shell commands via fabric api? according to many sources, it's quite tricky and not getting much details of it. – S.K. Venkat Mar 21 '18 at 14:03
1

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)
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • Thanks @StephenRauch for your answer. This was my initial question on bash: http://unix.stackexchange.com/questions/345374/how-to-get-the-last-words-of-the-line-in-log it might help to an accurate python solution. Thanks a billion for your time and support. – Omid1989 Feb 18 '17 at 07:47
  • 1
    @Omid1989 - OH, you left off the `-f` in your example above.... Now it does make a bit more sense. – Stephen Rauch Feb 18 '17 at 07:51
  • yes I removed `-f` because I want to send the `x8` variable via SPI if a certain code is received. (Raspberry Pi 3) – Omid1989 Feb 18 '17 at 08:01
  • Thanks @StephenRauch. The output of `omxd S` is something like `Playing 0/22 /myfolder/F02_Car_101.mp4`. Does this python code get `101` as the `before_dot` !!? – Omid1989 Feb 18 '17 at 08:17
  • Thanks @StephenRauch. I modified your code. Now it works great. Thanks. – Omid1989 Feb 18 '17 at 08:46