LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   python: can you update the current shell from python? (http://www.linuxquestions.org/questions/programming-9/python-can-you-update-the-current-shell-from-python-617516/)

BrianK 01-30-2008 08:00 PM

python: can you update the current shell from python?
 
Can I update the current shell with a python script?

The easy example would be an environment variable. Can I set an environment variable in a python script & make it stick after the python script exits?

What I'm actually trying to do is update the "complete" alias with a python script. For instance, this line does it when run on the command line:

complete foo 'p/1/(yabba dabba dooo)/'

... this will make it so that when you type "foo <tab>" you get the possible completions of "yabba" "dabba" or "dooo". (at least, that's how you do it in tcsh, haven't checked bash)

If I put that in a file, it works as long as I source the file as opposed to setting its exec bit & running it, which will not work.

I can run scripts from within python, but I'm not sure how to make python's changes stick in the current shell.

Any ideas?

rubadub 01-30-2008 09:56 PM

yes... python is can operate at the same level as any other terminal process, here's a very simple example of executing system commands (no capture of results other than exit code):
Code:

import os

iret = os.system("ls -al")
if iret != 0:
        print "Error - child terminated with signal ", iret


BrianK 01-30-2008 10:11 PM

Quote:

Originally Posted by rubadub (Post 3040606)
yes... python is can operate at the same level as any other terminal process, here's a very simple example of executing system commands (no capture of results other than exit code):
Code:

import os

iret = os.system("ls -al")
if iret != 0:
        print "Error - child terminated with signal ", iret


yes, that runs a system command, but it does nothing to my current shell.

for example:

Code:

os.system("setenv FOO bar")
will set an env var (in tcsh) for the time that python is running, but as soon as the python script terminates, so does that env var. I'm looking for a way to keep that env var around, or, more specifically, to keep a complete alias around.

paddy3118 01-30-2008 11:02 PM

Quote:

Originally Posted by BrianK (Post 3040528)
Can I update the current shell with a python script?

I can run scripts from within python, but I'm not sure how to make python's changes stick in the current shell.

Any ideas?

Sure arrange to source the output of a Python script then the Python script can output the text to set an environment variable (or three), and that will be sourced by the outer shell so changing the environment for subsequent commands.

- Paddy.

rubadub 01-31-2008 12:18 AM

well i'm having trouble with lot! I can't seem to do 'export LFS=/mnt/lfs', use declare or setenv (setenv is giving errors), even straight from shell? It's 6.10am an i'm off to bed, i'll look again tomorrow>>


All times are GMT -5. The time now is 02:38 PM.