-   Linux - Newbie (
-   -   Help calling a shell script inside a python script (

WyoChuck 05-26-2012 05:41 PM

Help calling a shell script inside a python script
Hi there,

I'm currently trying to write a small program using both Python and Linux shell script where I use the shell script to create a document that will be read by another program (which I didn't write but need to use for my project). I've figured out how to do this sucessfully but I need to change a variable in this script after every run through and as shell doesn't support math I've created the script for that in python.

To accomplish this I've written the following python script:

#!/usr/bin/env python

x= (i * 10E-6)
MASS = .08 + x

import subprocess"/d/users/charlest/mercury/testScripts/",shell=True)
where is my shell script to create the document. In my shell script I need to incriment the mass varaible though, how do I pass the variable on to the shell script?

And I know i haven't added a way to incriment the counting variable yet, I'll do that after the initial script works.

Thanks in advance, Chuck

David the H. 05-27-2012 06:59 AM

There's no way for one process to directly set an environmental value inside another. Your script can only set the variable from inside itself, although it is possible to receive the value from the outside.

This means you have to run the command as a sub-process of the script, using command substitution or similar, or else have it output the value to an external file or named pipe, and it's up to the script to figure out how to extract it from there.

Now, the shell can do integer-based math internally, but as you are aware it can't handle floating-point values. For those, you have to use an external process. Most scripters use bc or awk for that. If you really want to use your python command as the calculator, just substitute it for one of these other processes.

How can I calculate with floating point numbers instead of just integers?

WyoChuck 05-28-2012 04:19 PM

Okay... I'm new to using shell for arithmetic so how exactly would I do this?

I need my mass to begin at .08 and increment by 10E-6. Coding for this in Python was very simple. Is there a way that I'm simply missing to write a file from python much like the shell sript

echo "text to write" > filetowriteto.file

chrism01 05-28-2012 05:40 PM

If(!) I understand you correctly, the simplest soln would be to do it all in Python, including creating the 'document' from Python.
It might help if you described or (better) showed the doc you want to create.

WyoChuck 05-28-2012 09:39 PM

Thanks for the help, I decided to abondon using a shell script and do it all in one python script and after a few hours of Googling came up with the solution.

By the way for those of you that may be curious I ended up using


f = open('file', 'w')
f.write('code to be written')
f.write('next line to write')

All times are GMT -5. The time now is 11:31 AM.