Read Bash environment variable in TCL
Asked Answered
tcl
K

4

28

How to read a shell environment variable in your Tcl script. So anyone please help me. I am very new in TCL.

Keshiakesia answered 1/4, 2011 at 8:44 Comment(0)
D
37

Use $::env to access any environment variables e.g. to access the TMP environment variable do this:

set tmpdir $::env(TMP)

More info here http://wiki.tcl.tk/1624

Dodgson answered 1/4, 2011 at 8:54 Comment(4)
Just i write your code and execute it. But i am not getting. I am in tclsh prompt. My prompt is %. so what i did just type in the prompt % set tmpdir $::env(TMP). The output is like that can't read "::env(TMP)": no such variableKeshiakesia
Actually i want to write a proc is tcl that returns the Bash environment variable values.Keshiakesia
That's because you have no environment variable called TMP. Did you read the link I gave you?Dodgson
Ah, I can see the conversation you're having with the other responder - I think you'll be ok!Dodgson
E
5
$ export var=42
$ tclsh
% puts $env(var)
42
Entrance answered 1/4, 2011 at 8:56 Comment(11)
TMP environment variable is define in the bash script. so what i want to do just access the environment variable in the tcl. my tcl prompt is %. so finally i want to access the environment variable in tcl.Keshiakesia
The TMP enviroment variable would be $env(TMP). But: Is the tcl script called from the bash script? Is the variable global (ie. export)?Entrance
Yea. It is working fine. but my question is that what ever environment variable is defined in bash or shell script just i want to access using the proc in tcl. so please........Keshiakesia
yea. TMP variable is the environment variable that is defined in bash script. TMP=/tmp ORACLE_SID=DBTEST ORACLE_HOME=/opt/oracle/product/102 export ORACLE_SID ORACLE_HOME so here i want to access the environment variable ORACLE_SID and ORACLE_HOME, so how i should do, please ........Keshiakesia
So you want a list of all defined shell variables?Entrance
See what i am doing here. I am writing my all steps:Keshiakesia
It's all analogous: The bash variable ORACLE_SID can be accessed using $env(ORACLE_SID) in your tcl script.Entrance
Please just edit your question with the steps, that will be the easiest.Entrance
See what i am doing here. I am writing my all steps: 1. I am using linux 5.0 server. so i am in admin prompt and my prompt is #. My Prompt is [root@db1~] #. 2. after that what i did type tclsh. 3. so now i am in tcl prompt i.e. % (percentage) 4. Type here: su - oracle 5. after that my current working directory is /home/oracle/ and my prompt is $(Dollar). 6. now i again type here tclsh 7. so now i am in % prompt and after that final step 8. puts $env(ORACLE_SID) 7. so here i am getting the exact output DBTEST. so there are the process i am doing here. But what i want i want to write a tcl procKeshiakesia
so finally i want to write a tcl proc to return the bash environment variable. please help meKeshiakesia
Your problem seems to be the su rather than reading out the variable. You can't just call "su - oracle" inside of a script and read out the bash variables. The easiest way I can suggest is running your tcl script as 'su - oracle -c "tclsh /absolute/path/your-script.tcl"' - you can write a bash wrapper around that of course.Entrance
O
3

Environment variables are accessible via the built-in global variable env (fully qualified it is ::env). You use this like any other Tcl array.

If you want to print a list of all environment variables you can use something like this:

proc dump_env_vars {} {
    foreach name [array names ::env] {
        puts "$name == $::env($name)"
    }
}

Of course, to access just a single variable you use it like any other array, for example:

puts "HOME = '$::env(HOME)'"

For more information see the env page on the Tcler's wiki and the env section of the tclvars man page

Orvas answered 1/4, 2011 at 16:41 Comment(4)
yea, your procedure is executed, it gives all the environment variables. its ok. But what i am asking here. Suppose in bash script if i define the user define environment like this : ORACLE_SID=DBTEST ORACLE_HOME=/opt/oracle/product/102 export ORACLE_SID ORACLE_HOME Now i want to write a proc in tcl to access the environment variable that is defined by user. so how can i do it. please..........Keshiakesia
I also read u r link. But it is not match with my requirement.Keshiakesia
I just use parray env when I want to dump environment variables.Unemployed
Here Actually parray is used for print the env values. But what i want how to access the environment variable in TCL, that is defined in bash script, so please help me......Keshiakesia
S
2

To read a shell environment variable in Tcl script, try doing something like this:

global env
set olddisplay $env(DISPLAY)
set env(DISPLAY) unix:0

This might be expressed as well in this fashion:

set olddisplay $::env(DISPLAY)
set ::env(DISPLAY) unix:0

and forget about global.

You can check to see if the variable exists by doing something like:

if {[info exists env(VARNAME)]} {
    # okay, it's there, use it
    set value $env(VARNAME)
} else {
    # the environment var isn't set, use a default
    set value "the default value"
}

This is source.

Schistosome answered 17/4, 2015 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.