execute command in CURRENT shell

  • Thread starter Valentine Kouznetsov
  • Start date
V

Valentine Kouznetsov

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

Rob Williscroft

Valentine Kouznetsov wrote in @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.
 
R

Rich Drewes

Rob said:
Valentine Kouznetsov wrote in @posting.google.com:
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
 
G

Grant Edwards

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.
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top