execute a shell script from a python script

Discussion in 'Python' started by spec, Jul 17, 2006.

  1. spec

    spec Guest

    Hi all, I know nothing about Python. What I need to do is to get a
    Python script to execute a local shell script. I do not need any
    output. What would be th eeasiest way to accomplish this?

    Thanks!
     
    spec, Jul 17, 2006
    #1
    1. Advertising

  2. If your script is foo.sh and takes args:
    import subprocess
    subprocess.call(["foo.sh","args"],shell=True)
    Should work fine. check out
    http://www.python.org/dev/doc/maint24/lib/module-subprocess.html

    Enjoy,
    THN

    spec wrote:
    > Hi all, I know nothing about Python. What I need to do is to get a
    > Python script to execute a local shell script. I do not need any
    > output. What would be th eeasiest way to accomplish this?
    >
    > Thanks!
     
    Thomas Nelson, Jul 17, 2006
    #2
    1. Advertising

  3. spec

    spec Guest

    Thanks, actually there are no args, is there something even simpler?

    Thanks
    Frank


    Thomas Nelson wrote:
    > If your script is foo.sh and takes args:
    > import subprocess
    > subprocess.call(["foo.sh","args"],shell=True)
    > Should work fine. check out
    > http://www.python.org/dev/doc/maint24/lib/module-subprocess.html
    >
    > Enjoy,
    > THN
    >
    > spec wrote:
    > > Hi all, I know nothing about Python. What I need to do is to get a
    > > Python script to execute a local shell script. I do not need any
    > > output. What would be th eeasiest way to accomplish this?
    > >
    > > Thanks!
     
    spec, Jul 18, 2006
    #3
  4. On Mon, 2006-07-17 at 16:59 -0700, spec wrote:
    > Thanks, actually there are no args, is there something even simpler?
    >
    > Thanks
    > Frank
    >
    >
    > Thomas Nelson wrote:
    > > If your script is foo.sh and takes args:
    > > import subprocess
    > > subprocess.call(["foo.sh","args"],shell=True)
    > > Should work fine. check out
    > > http://www.python.org/dev/doc/maint24/lib/module-subprocess.html
    > >
    > > Enjoy,
    > > THN
    > >
    > > spec wrote:
    > > > Hi all, I know nothing about Python. What I need to do is to get a
    > > > Python script to execute a local shell script. I do not need any
    > > > output. What would be th eeasiest way to accomplish this?
    > > >
    > > > Thanks!

    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    Check out os.popen4 or the commands module.




    --
    This message has been scanned for viruses and
    dangerous content by MailScanner, and is
    believed to be clean.
     
    John McMonagle, Jul 18, 2006
    #4
  5. spec

    Simon Forman Guest

    spec wrote:
    > Thanks, actually there are no args, is there something even simpler?
    >
    > Thanks
    > Frank


    you could try os.system()

    >From the docs:


    system(command)
    Execute the command (a string) in a subshell. This is implemented
    by calling the Standard C function system(), and has the same
    limitations. Changes to posix.environ, sys.stdin, etc. are not
    reflected in the environment of the executed command.

    On Unix, the return value is the exit status of the process encoded
    in the format specified for wait(). Note that POSIX does not specify
    the meaning of the return value of the C system() function, so the
    return value of the Python function is system-dependent.

    On Windows, the return value is that returned by the system shell
    after running command, given by the Windows environment variable
    COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always
    0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status
    of the command run; on systems using a non-native shell, consult your
    shell documentation.

    Availability: Macintosh, Unix, Windows.
     
    Simon Forman, Jul 18, 2006
    #5
  6. In article <>,
    Simon Forman <> wrote:
    >spec wrote:
    >> Thanks, actually there are no args, is there something even simpler?
    >>
    >> Thanks
    >> Frank

    >
    >you could try os.system()
    >
    >>From the docs:

    >
    >system(command)

    .
    [more detail]
    .
    .
    I'm concerned the follow-ups in this thread have been too subtle.
    Here is what you need to know: use system(). A model such as

    import os
    os.system("my_script")

    fulfills exactly the requirements the original poster described.
     
    Cameron Laird, Jul 18, 2006
    #6
  7. As described in the docs I pointed to before:
    subprocess.call("foo.sh",shell=True)
    Is the way to do it without args. I think it is simplest to learn the
    subprocess module because (quoting from the docs) this module intends
    to replace several other, older modules and functions, such as:
    os.system
    os.spawn*
    os.popen*
    popen2.*
    commands.*
    This way you only need to learn one thing. Actually I would like to
    see some of these older functions deprecated.

    THN

    Cameron Laird wrote:
    > In article <>,
    > Simon Forman <> wrote:
    > >spec wrote:
    > >> Thanks, actually there are no args, is there something even simpler?
    > >>
    > >> Thanks
    > >> Frank

    > >
    > >you could try os.system()
    > >
    > >>From the docs:

    > >
    > >system(command)

    > .
    > [more detail]
    > .
    > .
    > I'm concerned the follow-ups in this thread have been too subtle.
    > Here is what you need to know: use system(). A model such as
    >
    > import os
    > os.system("my_script")
    >
    > fulfills exactly the requirements the original poster described.
     
    Thomas Nelson, Jul 18, 2006
    #7
  8. In article <>,
    Thomas Nelson <> wrote:
    >As described in the docs I pointed to before:
    >subprocess.call("foo.sh",shell=True)
    >Is the way to do it without args. I think it is simplest to learn the
    >subprocess module because (quoting from the docs) this module intends
    >to replace several other, older modules and functions, such as:
    >os.system
    >os.spawn*
    >os.popen*
    >popen2.*
    >commands.*
    >This way you only need to learn one thing. Actually I would like to
    >see some of these older functions deprecated.

    .
    .
    .
    A point worth repeating, and I salute your courtesy in doing so.
    I had realized neither the deprecation of these interfaces, nor
    the documentation to that effect, so I thank you for pointing
    them out.
     
    Cameron Laird, Jul 18, 2006
    #8
    1. Advertising

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. Christian Heimes
    Replies:
    0
    Views:
    610
    Christian Heimes
    Feb 27, 2008
  2. Gerardo Herzig
    Replies:
    1
    Views:
    1,105
    Philipp Pagel
    Feb 27, 2008
  3. D'Arcy J.M. Cain
    Replies:
    0
    Views:
    879
    D'Arcy J.M. Cain
    Feb 27, 2008
  4. kderaedt

    Execute shell script from a perl script

    kderaedt, Jun 26, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    179
    Andres Monroy-Hernandez
    Jun 26, 2003
  5. moongeegee

    execute a shell script in a shell script

    moongeegee, Dec 3, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    258
    Ben Morrow
    Dec 4, 2007
Loading...

Share This Page