solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

passing data from .py to .sh

Thu Nov 17, 2016 3:10 pm

I know very little about programing so please bare with me!


I'm trying to pass some data from a python program to a shell script. I understand one option is to "call" the shell script from within the python program. I have tried the following but I'm getting an error.

Code: Select all

#Python 2.7.9 (default, Mar  8 2015, 00:52:26) 
#[GCC 4.9.2] on linux2
#Type "copyright", "credits" or "license()" for more information.

#!/bin/python
#program to read temp.txt file and deliver...
f = open('/var/www/axx/temp.txt')
line1 =f.readline()
line2=f.readline()

f.close()

print 'line1',line1
print 'line2',line2

call(['bash', 'overlayv2.sh', line1, line2])
The result includes this error:

line1 11/15/2016 05:15:00 PM

line2 50.0F

Traceback (most recent call last):
File "/home/pi/axx/datatransv1.0.py", line 32, in <module>
call(['bash', 'overlayv2.sh', line1, line2, line3])
NameError: name 'call' is not defined
>>>

Can someone explan what I'm doing wrong?
Also, If there is a better (easrer) way I'm all ears!

THANKS FOR ANY HELP!!!
I'm a total novice, non-programer (...basically a hack.)

SkyRise
Posts: 179
Joined: Tue Jan 24, 2012 1:20 pm

Re: passing data from .py to .sh

Thu Nov 17, 2016 3:26 pm

You want to use subprocess.run, like

Code: Select all

subprocess.run(["ls", "-l"])
And see the docs at
https://docs.python.org/3/library/subprocess.html

solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

Re: passing data from .py to .sh

Thu Nov 17, 2016 3:46 pm

I get the same error when I try this:

Code: Select all

subprocess.run(['overlayv2.sh', line1, line2, line3])
Or this:

Code: Select all

subprocess.run(['bash','overlayv2.sh', line1, line2, line3])

???
I'm a total novice, non-programer (...basically a hack.)

SkyRise
Posts: 179
Joined: Tue Jan 24, 2012 1:20 pm

Re: passing data from .py to .sh

Thu Nov 17, 2016 4:02 pm

Sorry, hadn't noticed that you're using python 2.7

It did used to be 'call':

Code: Select all

subprocess.call(["ls", "-l"])
See python 2 docs here
https://docs.python.org/2/library/subprocess.html

solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

Re: passing data from .py to .sh

Thu Nov 17, 2016 4:34 pm

Thanks... back to original question... what am I doing wrong?
I'm a total novice, non-programer (...basically a hack.)

SkyRise
Posts: 179
Joined: Tue Jan 24, 2012 1:20 pm

Re: passing data from .py to .sh

Thu Nov 17, 2016 4:51 pm

You're using

Code: Select all

call(['bash', 'overlayv2.sh', line1, line2])
instead of

Code: Select all

subprocess.call(['bash', 'overlayv2.sh', line1, line2])
The 'call' function is part of the 'subprocess' package. Which does raise another point, you'll need to import the 'subprocess' package in order to use its functions (at the top of your code)

Code: Select all

import subprocess

solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

Re: passing data from .py to .sh

Thu Nov 17, 2016 11:25 pm

Ok... making progress...
adding

Code: Select all

import subprocess
eliminated the error... and the shell script runs but it doesn't seem to pass the contents of line1, line2 etc?

Any ideas...?
I'm a total novice, non-programer (...basically a hack.)

solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

Re: passing data from .py to .sh

Fri Nov 18, 2016 5:50 pm

anybody? anything?
I'm a total novice, non-programer (...basically a hack.)

Return to “Beginners”