matzz
Posts: 2
Joined: Mon Sep 10, 2012 3:24 pm

Running .sh program using python

Mon Sep 10, 2012 3:31 pm

Hi,

I'm completly new to Python. I've learned some languages at school and now I'm looking for way to program and customize RPI as I need for myself.

I'm planning to use RPi in my car as multimedia center. This Multimedia center has to have also possibility to control my cars ECU. I have Megasquirt ecu in my car and I'm looking for a way to run MegaTunix from XBMC main screen with one single click.

In order to do so I need to edit stock XBMC skin to make another button and clicking it leads to running MegaTunix.

Im looking for help from here.

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Running .sh program using python

Tue Sep 11, 2012 5:52 am

@matzz: I know nothing about programming/extending XMBC but the `subprocess` module is the answer to the question (?) in the subject line of your post.

Code: Select all

while not self.asleep():
    sheep += 1

matzz
Posts: 2
Joined: Mon Sep 10, 2012 3:24 pm

Re: Running .sh program using python

Tue Sep 11, 2012 1:58 pm

Thank you, I'll try to look more into it soon. If I make any progress, I'll post it here also.

ElectronicsGeek
Posts: 20
Joined: Sun Sep 16, 2012 8:28 am

Re: Running .sh program using python

Sun Sep 16, 2012 8:40 am

I don't know anything at all about programming XBMC, but if you want to automate shell commands in python, I can show you how to do that. There is a library for python called os that will do this (you may have to download it, I don't know as I haven't tried it on a raspi)

Code: Select all

import os # Import the os library

os.system("SHELL COMMAND GOES INSIDE QUOTES")
WIth that, you should be able to run shell commands within python. If you wanted to run a shell script in it, you would just enter, "sh NAME OF SHELL SCRIPT.sh". And that's pretty much all there is to it. Hope that this is of relevance, good luck with your project :D.

ACP_
Posts: 33
Joined: Sat Aug 18, 2012 10:40 pm

Re: Running .sh program using python

Sun Sep 16, 2012 10:16 am

Hi,
While os.system() works it is considered a bad pratice to use it and subprocess module should be used in favor of some of old os.* calls. Despite Python recommendations found in official documentation: http://docs.python.org/library/subprocess.html there is another great reason not to use it or at least be very carefull when you pass unvalidated/untrusted input to os.system(). Also please note that subprocess calls generally do not invoke shell unless shell=True is being used explicitly.

Here is a good writeup with examples regarding insecure use of os.system():
http://www.hpenterprisesecurity.com/vul ... ction.html

User avatar
aboudou
Posts: 98
Joined: Wed Aug 08, 2012 12:38 pm
Location: Cintegabelle, France
Contact: Website

Re: Running .sh program using python

Sun Sep 16, 2012 12:23 pm

matzz wrote: I'm planning to use RPi in my car as multimedia center. This Multimedia center has to have also possibility to control my cars ECU. I have Megasquirt ecu in my car and I'm looking for a way to run MegaTunix from XBMC main screen with one single click.
Hi,

You have a look to https://github.com/aboudou/raspiledmete ... er/main.py (starting line #52) to see how you may execute an external script, and check for the output.
https://goddess-gate.com/ - My main website
http://raspberrypi.goddess-gate.com/ - A website about (and hosted by) my Raspberry Pi
https://twitter.com/boudouarnaud - Follow me on Twitter
http://www.fablab-sud31.fr/ - Fab Lab Sud31-Val d'Ariège

Return to “Python”