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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving Unix Environment Variables (on Solaris 9)

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
I'm having "brain freeze" ...

I just want to see the contents of some of my unix environment variables within tcl - what's the correct syntax?

Tried ...
> puts [exec {echo $USER}]


...I don't recall if this can be done?!?
 
Environment variables are available in Tcl by means of the env array. So to get the value of the USER variable, if there is one, you would access $env(USER). To get the path, you would use $env(PATH). And so on.

_________________
Bob Rashkin
rrashkin@csc.com
 
Bong, I knew you'd be listening out there.

I've tried
> puts [exec echo $env(USER)]
but it says "no such variable".

The $USER variable is "out there" in my unix environment, but how do I get it in tcl - I mean syntax-wise, what am I missing?

BTW - When you say "...if there is one", you do mean in unix ...right?
 
What I mean is, if it's an environment variable. Try this, in a Tcl shell use array get to turn the entire envrionment array into a list: set envlist [array get env]. Now, envlist will have pairs of elements in the form "variable name" "value".

If "USER" is there, try puts $env(USER). I think you're trying to use Tcl syntax in a UNIX command (with exec) and that might be your problem.

_________________
Bob Rashkin
rrashkin@csc.com
 
There is no need for any execs. Tcl has a copy of your user
environment in the array env. Try parray env.
 
Well ....

When I interactively run wish, at the prompt I enter "parray env" and it gives me all my variables ... goooood ...

BUT ....

When running a script, which uses the same wish exe, I'm getting error msg's:

> puts [parray env]
MSG: env isn't an array

> puts [parray $env]
MSG: "can't read env , no such varaible" ...

Am I forgetting to set something globally in my script?

 
puts" and "parray" may be redundant.

Anyway, in your Tcl script, try settng a variable to $env(USER). Then see if you can work with that variable.

_________________
Bob Rashkin
rrashkin@csc.com
 
Bong --

Yep, U R right ... puts and parray are redundant ...

Anyway, I tried your quick check and sure enough that worked:

>set env(USER) "DOGGY"
>parray env


So I'm back to my original problem, why can't I get to my env variables when running my script? What am I not including in my script? Why am I able to see all my env variables when running interactively?

Just for background info. - I'm starting wish like this in my script:

#! /bin/ksh
# blah blah \
exec /somedir/somedir/wish "$0" "$@"

Note: I just tried running script w/o the lines above and running via the command line - no difference, same results!!

but I'm not setting any tcl-like variables within my script ..


 
I'm not sure but I think you may be still in the Korn shell when you think you're giving commands in the Tk shell. I don't know how it is on UNIX, but on Windows, if I invoke Wish from a command console, I will now have 2 consoles: 1 DOS and 1 Wish. I don't know the syntax but I think you need to pass a script to Wish when you call it up from the Kshell.

_________________
Bob Rashkin
rrashkin@csc.com
 
... I've always been passing a script to wish when I call it up from a ksh. I was just explaining my start-up environment in case that provided any hints to the problem ...


So I'm still stuck !!!!
 
It was a global thing ...

I need to either :

1.) In my proc, I need to define "env" as global, or
2.) reference the "env" variables individually - e.g. -$::env(PATH)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top