LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-30-2008, 08:00 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
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?
 
Old 01-30-2008, 09:56 PM   #2
rubadub
Member
 
Registered: Jun 2004
Posts: 233

Rep: Reputation: 33
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
 
Old 01-30-2008, 10:11 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by rubadub View Post
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.
 
Old 01-30-2008, 11:02 PM   #4
paddy3118
LQ Newbie
 
Registered: Jul 2007
Location: Bristol UK
Distribution: RedHat, Cygwin
Posts: 14

Rep: Reputation: 0
Quote:
Originally Posted by BrianK View Post
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.
 
Old 01-31-2008, 12:18 AM   #5
rubadub
Member
 
Registered: Jun 2004
Posts: 233

Rep: Reputation: 33
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>>
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Fedora 2 04-23-2009 07:02 PM
LXer: Charming Python: Python elegance and warts, Part 2 LXer Syndicated Linux News 0 08-31-2007 08:40 AM
python, os.system() function. howto use python variables? jhwilliams Programming 5 07-28-2007 01:56 AM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM
python problem - compiled from source - python -V still showing old version txm123 Linux - Newbie 1 02-15-2006 11:05 AM


All times are GMT -5. The time now is 09:27 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration