execute command in CURRENT shell

Discussion in 'Python' started by Valentine Kouznetsov, Aug 8, 2003.

  1. Hi,
    simple question, how to execute command in current shell, not in
    subshell?

    Example. I have environment variable A=aaa and need to invoke shell
    (sh) script which will do something with A. If I do os.system() then
    sub-shell will setup A
    and execute my script there leaving A in parent shell untouched.

    Thanks,
    Valentine.
    Valentine Kouznetsov, Aug 8, 2003
    #1
    1. Advertising

  2. Valentine Kouznetsov wrote in news:ec6d1a16.0308080828.367f6b54
    @posting.google.com:

    > Hi,
    > simple question, how to execute command in current shell, not in
    > subshell?
    >
    > Example. I have environment variable A=aaa and need to invoke shell
    > (sh) script which will do something with A. If I do os.system() then
    > sub-shell will setup A
    > and execute my script there leaving A in parent shell untouched.
    >


    A hack for you:

    Create a new sh script. say new.sh:

    #!/bin/sh
    source $*
    echo Result=$A
    # ----------------


    #!/bin/python
    import os

    command="original.sh arg1, arg2" # you get the idea

    #Sorry don't know how you need to invoke sh
    w = os.popen( "sh ./new.sh " + command )

    while ( True ):
    s = w.readline()
    if not s:
    break
    if s.startswith( "Result=" ):
    os.environ[ "A" ] = s[ s.find( "=" ) + 1 : ] )
    break
    # Or maybe read all lines - so you get the last "Result="!

    w.close()

    HTH

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Aug 8, 2003
    #2
    1. Advertising

  3. Valentine Kouznetsov

    Rich Drewes Guest

    Rob Williscroft wrote:

    >Valentine Kouznetsov wrote in news:ec6d1a16.0308080828.367f6b54
    >@posting.google.com:
    >
    >
    >
    >>Hi,
    >>simple question, how to execute command in current shell, not in
    >>subshell?
    >>

    execl() will execute a new program in the current process, replacing the
    current process. It obviously cannot return back to Python. See
    http://www.python.org/doc/2.2/lib/os-process.html .

    spawnl() will execute a new program in a new process, but it does not
    invoke a new *shell*. It allows you to completely specify the
    environment, so this may also do what you want. Unlike execl() you can
    return to Python after the subprocess is complete.

    Rich
    Rich Drewes, Aug 8, 2003
    #3
  4. In article <>, Rich Drewes wrote:

    >>>simple question, how to execute command in current shell, not
    >>>in subshell?

    >
    > execl() will execute a new program in the current process,
    > replacing the current process.


    Right. However, it still can't do anthing in or to the current
    shell, since the current process is already a child of the
    "current shell". IOW, it can't modify the current shell's
    environment (the environment of the parent process) which is
    what I understood to be the OP's desire.

    --
    Grant Edwards grante Yow! My BIOLOGICAL ALARM
    at CLOCK just went off... It
    visi.com has noiseless DOZE FUNCTION
    and full kitchen!!
    Grant Edwards, Aug 8, 2003
    #4
    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. Replies:
    2
    Views:
    5,635
    Jonathan Bromley
    Feb 18, 2005
  2. Replies:
    4
    Views:
    1,685
    Terry Hancock
    Apr 23, 2005
  3. nadz
    Replies:
    4
    Views:
    800
    Alf P. Steinbach
    Apr 20, 2006
  4. moongeegee

    execute a shell script in a shell script

    moongeegee, Dec 3, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    230
    Ben Morrow
    Dec 4, 2007
  5. sagar varule
    Replies:
    6
    Views:
    118
    Joshua Landau
    Aug 11, 2013
Loading...

Share This Page