Returning Values from Bash Scripts

C

chakkaradeepcc

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
 
W

William Park

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 <[email protected]>, 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/
 
M

Mike Meyer

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
 
S

Steven D'Aprano

The easy way is to call it with subprocess.call.
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:
0

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

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.
 
T

Tim Roberts

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.
 
J

Jorgen Grahn

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
 
M

Mike Meyer

Jorgen Grahn said:
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
 
F

Fredrik Lundh

Steven said:
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/trunk/Lib/subprocess.py

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top