Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Returning Values from Bash Scripts

Reply
Thread Tools

Returning Values from Bash Scripts

 
 
chakkaradeepcc@gmail.com
Guest
Posts: n/a
 
      01-06-2006
HI all,

How to execute bash scripts from python (other than using os.popen) and
get the values that those bash scripts return.

I would be happy if someone could help me out in this..

thanks in advance..

With Regards,
Chakkaradeep

 
Reply With Quote
 
 
 
 
William Park
Guest
Posts: n/a
 
      01-08-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> HI all,
>
> How to execute bash scripts from python (other than using os.popen) and
> get the values that those bash scripts return.
>
> I would be happy if someone could help me out in this..


Well, if stdout is not an option, then save it to file (text, GDBM,
Python source format, etc) and read it back in Python.

--
William Park <(E-Mail Removed)>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
 
Reply With Quote
 
 
 
 
Mike Meyer
Guest
Posts: n/a
 
      01-08-2006
(E-Mail Removed) writes:

> How to execute bash scripts from python (other than using os.popen) and
> get the values that those bash scripts return.


The easy way is to call it with subprocess.call.

<mike
--
Mike Meyer <(E-Mail Removed)> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-08-2006
On Sat, 07 Jan 2006 22:03:36 -0500, Mike Meyer wrote:

> (E-Mail Removed) writes:
>
>> How to execute bash scripts from python (other than using os.popen) and
>> get the values that those bash scripts return.

>
> The easy way is to call it with subprocess.call.



>>> import subprocess

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named subprocess


It might be easy, but an awful lot of people won't be using a version of
Python that has the subprocess module, and for them upgrading may not be
easy (or even possible) at all.

For those that don't have access to subprocess, there is an embarrassment
of riches available. You can do this:

>>> result = os.system('ls')

file.py file.txt
>>> result

0

If you want to capture the result of the command, you can try this:

>>> output = os.popen('ls -l').read()


Perhaps the simplest way if subprocess is not available to you is the
commands module. Other possibilities are os.fork, os.execv and the popen2
module. There may be other solutions as well.



--
Steven.

 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      01-08-2006
(E-Mail Removed) wrote:
>
>How to execute bash scripts from python (other than using os.popen) and
>get the values that those bash scripts return.


Why would you eliminate os.popen? It is precisely the right way to do
this. That's the same interface bash itself uses to execute scripts.

That is, assuming by "values" you mean the stdout from the script. If you
really mean the numerical return code, you can use os.system.
--
- Tim Roberts, (E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      01-08-2006
On Sun, 08 Jan 2006 08:57:01 GMT, Tim Roberts <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
>>
>>How to execute bash scripts from python (other than using os.popen) and
>>get the values that those bash scripts return.

>
> Why would you eliminate os.popen? It is precisely the right way to do
> this. That's the same interface bash itself uses to execute scripts.
>
> That is, assuming by "values" you mean the stdout from the script. If you
> really mean the numerical return code, you can use os.system.


And that's just one "value", of course. And not a very useful one, either --
it's a non-negative integer, with a pretty low max value -- 255 on my
machine. Unless you count the crash return codes.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
 
Reply With Quote
 
Mike Meyer
Guest
Posts: n/a
 
      01-08-2006
Jorgen Grahn <(E-Mail Removed)> writes:
> On Sun, 08 Jan 2006 08:57:01 GMT, Tim Roberts <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>>How to execute bash scripts from python (other than using os.popen) and
>>>get the values that those bash scripts return.

>> Why would you eliminate os.popen? It is precisely the right way to do
>> this. That's the same interface bash itself uses to execute scripts.
>> That is, assuming by "values" you mean the stdout from the script. If you
>> really mean the numerical return code, you can use os.system.

> And that's just one "value", of course. And not a very useful one, either --
> it's a non-negative integer, with a pretty low max value -- 255 on my
> machine. Unless you count the crash return codes.


Further, os.system (and os.popen) pass the command through a shell,
and actually gives you the return value from the shell instead of the
command in question. Normally, these are the same thing. But if the
shell fails to execute the command, they aren't - and there's no easy
way to tell that that's what happened.

<mike
--
Mike Meyer <(E-Mail Removed)> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-08-2006
Steven D'Aprano wrote:

> >> How to execute bash scripts from python (other than using os.popen) and
> >> get the values that those bash scripts return.

> >
> > The easy way is to call it with subprocess.call.

>
>
> >>> import subprocess

> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> ImportError: No module named subprocess
>
> It might be easy, but an awful lot of people won't be using a version of
> Python that has the subprocess module, and for them upgrading may not be
> easy (or even possible) at all.


subprocess is available as a separate distribution for Python 2.2
and newer:

http://www.lysator.liu.se/~astrand/popen5/

if you're on a unixoid system, all you need is the subprocess.py
file, which you can also get directly from the Python SVN:

http://svn.python.org/view/python/tr.../subprocess.py

</F>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can ruby replace bash scripts for linux script Greg Hauptmann Ruby 13 07-14-2008 08:09 AM
Running Python scripts from BASH Ishpeck Python 6 02-28-2007 04:04 PM
Stupid question: Making scripts python-scripts Jan Danielsson Python 8 07-22-2005 12:20 AM
(patch for Bash) GTK+2 + Bash William Park XML 3 07-14-2005 12:09 AM
Comparing values in 2 textfiles and returning the missing values Jorgen Gustafsson Perl 4 12-12-2003 09:09 AM



Advertisments