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

7
  • 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? Commented Feb 18, 2017 at 7:00
  • Why can't you put it in a script instead? Commented Feb 18, 2017 at 7:01
  • 1
    there is some good stuff in this post about using subprocess.pipe 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 Commented Feb 18, 2017 at 7:03
  • I don'w want to call external scripts inside python, because of accessing I/O SPI peripherals inside python. Commented Feb 18, 2017 at 7:04
  • 1
    Why not just read /var/log/omxlog and execute omxd directly in python? bash seems to be unnecessary here. Commented Feb 18, 2017 at 7:16

2 Answers 2

15

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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)

5 Comments

Thanks @StephenRauch for your answer. This was my initial question on bash: unix.stackexchange.com/questions/345374/… it might help to an accurate python solution. Thanks a billion for your time and support.
@Omid1989 - OH, you left off the -f in your example above.... Now it does make a bit more sense.
yes I removed -f because I want to send the x8 variable via SPI if a certain code is received. (Raspberry Pi 3)
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 !!?
Thanks @StephenRauch. I modified your code. Now it works great. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.