Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set enviroinment

Status
Not open for further replies.

naru21

Technical User
May 24, 2002
13
US
Hi,
How does one set the .profile environment in tcl
the equivalent command in shell

. /etc/.profile

could anyone tell me how i can fix the problem.
any help is appreciated.
 
Your environment variables are located in the env array.
try parray env.

You can reset these the same way you reset any other variable.
You can easily write a small procedure which tracks changes to the environment and restores them as well
though I am not sure (maybe Avia could say?) whether changes to the env array have system persistent effects,
or strictly tclsh local.
 
Well, two different issues here. In response to your question, marsd, changes to environment variable through the pre-defined global env array affect only the currently running process and any processes spawned by it. There's no way for a program to change environment variables and have those changes propagated into the calling context (without the cooperation of the calling context), because that would be a gaping security hole. Just imagine running a script that could change your PATH environment variable without your knowing it.

But naru21 seems to be asking about something else. I think he's asking if there's a way to have tclsh or wish automatically execute a set of commands on start-up, in rather that same way that you can have a ".profile" or ".bashrc" file. The answer is yes.

From the tclsh manual page:

"If there exists a file .tclshrc (or tclshrc.tcl on the Windows platforms) in the home directory of the user, tclsh evaluates the file as a Tcl script just before reading the first command from standard input."

Similarly, the wish manual page says:

"If there exists a file .wishrc in the home directory of the user, wish evaluates the file as a Tcl script just before reading the first command from standard input."

Actually, upon testing this on a Windows 2000 system, if there is a file named wishrc.tcl in the user's home directory, wish evaluates the commands contained in that file. So it is parallel in operation with tclsh. The documentation just needs updating.

Note that on all platforms, these startup scripts are executed only for interactive sessions; they aren't executed if you specify a script name when you start the interpreter (for example, executing "tclsh myscript.tcl"). Of course, the expectation is that you could write a script to do the same thing when it starts, if that's the behavior you want. - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Hi Avia,
i am working on unix box. what you said is correct when i run the script the script should automatically execute the .profile
I guess this can be done some thing like this but i am not sure how/what it is doing

my user name on unix box is
user name : naru

set env(Naruprof) naru

will this set the same environment of naru/.profile to the script if i run the script from another user.

Thanks

 
No, it doesn't work that way. As stated in the documentation, when you start an interactive tclsh session, it looks only in the current user's home directory.

One alternative you could explore is providing a "wrapper" to tclsh that people would use instead of tclsh itself. You could base it off the technique described on the Tcl'ers Wiki page "exec magic," Before actually starting the tclsh interpreter, you'd include some lines to set up the environment in any way that you wanted. I'm really rusty on my Bourne shell programming, but I was thinking of something like this:

Code:
#!/bin/sh

# Set up a new environment variable SOMEDIR=/here/it/is ; export SOMEDIR

# Start tclsh exec tclsh "$0" ${1+"$@"}
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Avia,
That's what I thought. Thanks.

Naru,
I think "source" is more of what you are looking for naru.
Though it might be script specific.

Signing off.
MD
 
Thanks Avia & MD

i thought of setting the environment from tcl but as suggested by Avia i am calling tcl script from a shell script.

Thanks.
-Naru..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top