execute command in CURRENT shell

Rob Williscroft rtw at freenet.REMOVE.co.uk
Fri Aug 8 20:46:25 CEST 2003


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/




More information about the Python-list mailing list