Returning Values from Bash Scripts

Discussion in 'Python' started by chakkaradeepcc@gmail.com, Jan 6, 2006.

  1. Guest

    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
     
    , Jan 6, 2006
    #1
    1. Advertisements

  2. William Park Guest

    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 <>, 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/
     
    William Park, Jan 8, 2006
    #2
    1. Advertisements

  3. Mike Meyer Guest

    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 <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Jan 8, 2006
    #3
  4. On Sat, 07 Jan 2006 22:03:36 -0500, Mike Meyer wrote:

    > 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.
     
    Steven D'Aprano, Jan 8, 2006
    #4
  5. Tim Roberts Guest

    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,
    Providenza & Boekelheide, Inc.
     
    Tim Roberts, Jan 8, 2006
    #5
  6. Jorgen Grahn Guest

    On Sun, 08 Jan 2006 08:57:01 GMT, Tim Roberts <> wrote:
    > 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!
     
    Jorgen Grahn, Jan 8, 2006
    #6
  7. Mike Meyer Guest

    Jorgen Grahn <> writes:
    > On Sun, 08 Jan 2006 08:57:01 GMT, Tim Roberts <> wrote:
    >> 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 <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Jan 8, 2006
    #7
  8. 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/trunk/Lib/subprocess.py

    </F>
     
    Fredrik Lundh, Jan 8, 2006
    #8
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Park

    (patch for Bash) GTK+2 + Bash

    William Park, Jul 11, 2005, in forum: XML
    Replies:
    3
    Views:
    2,390
    William Park
    Jul 14, 2005
  2. Jan Danielsson
    Replies:
    8
    Views:
    722
    Mike Meyer
    Jul 22, 2005
  3. Jp Calderone
    Replies:
    0
    Views:
    510
    Jp Calderone
    Jul 21, 2005
  4. Ishpeck

    Running Python scripts from BASH

    Ishpeck, Feb 27, 2007, in forum: Python
    Replies:
    6
    Views:
    541
  5. Greg Hauptmann
    Replies:
    13
    Views:
    271
    ruud grosmann
    Jul 14, 2008
Loading...

Share This Page