LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-26-2012, 06:41 PM   #1
WyoChuck
LQ Newbie
 
Registered: May 2012
Location: Wyoming
Distribution: Linux enterprise/Ubuntu
Posts: 3

Rep: Reputation: Disabled
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:
Quote:
#!/usr/bin/env python

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

import subprocess
subprocess.call("/d/users/charlest/mercury/testScripts/write2.sh",shell=True)
where write2.sh 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
 
Old 05-27-2012, 07:59 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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?
http://mywiki.wooledge.org/BashFAQ/022

Last edited by David the H.; 05-27-2012 at 08:11 AM. Reason: rewording for clarity
 
Old 05-28-2012, 05:19 PM   #3
WyoChuck
LQ Newbie
 
Registered: May 2012
Location: Wyoming
Distribution: Linux enterprise/Ubuntu
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
Code:
echo "text to write" > filetowriteto.file
 
Old 05-28-2012, 06:40 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
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.
 
Old 05-28-2012, 10:39 PM   #5
WyoChuck
LQ Newbie
 
Registered: May 2012
Location: Wyoming
Distribution: Linux enterprise/Ubuntu
Posts: 3

Original Poster
Rep: Reputation: Disabled
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

Code:
f = open('file', 'w')
f.write('code to be written')
f.write('\n')
f.write('next line to write')
.
.
.
f.close()
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to close open ports using a python script or a shell script in python ?? apanimesh061 Programming 3 11-20-2011 01:31 AM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
[SOLVED] calling bash script with more than one variable from python script mfb Programming 4 10-12-2010 07:51 AM
Runs Multiple shell script inside a main script using crontab srimal Linux - Newbie 4 10-22-2009 07:19 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-12-2005 12:08 AM


All times are GMT -5. The time now is 04:47 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration