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!

TCL -> Eval/Exec with Environment Variables

Status
Not open for further replies.

nsgill

Programmer
Feb 9, 2004
6
US
Ok. I have a problem in TCL that I hope someone here can help me out with. The problem is with using the 'exec/eval' command. I want to be able to execute a command where the command involves an environment variable. For example; lets say I want to say

echo $PATH

the problem is that the user is going to type in the 'command arguments' through the gui. So he/she will type in '$PATH'. The problem with that is that TCL sees that as its own local variable; due to the '$'.

The workraound would be to use the 'global env' array and write it as: echo $env(PATH) but the problem with that is that I dont know where the environment is going to appear in the arguments. The command could be:

% someProg -file $PATH/file1 $PATH/2

so it would get messy to do manual 'parsing' for a '$' and do subsitution.

Any built in tcl tricks??

thanks in advance!
Nav
 
If you don't want the $PATH be evaled by Tcl but by the shell, surround it with braces:
Code:
  exec someProg -file {$PATH/file1} {$PATH/2}
If you need to generate the command from other Tcl vars:
Code:
  set f1 file1
  set f2 2
  set cmd [format {someProg -file {$PATH/%s} {$PATH/%s}} $f1 $f2]
  eval exec $cmd
HTH

ulis
 
Thanks for the tip. But it still does not work for me.

Lets say I want to issue an 'ls' command on a directory that is defined in this env variable: "$CVSROOT"

I am trying it this way:

catch {exec ls {$CVSROOT}} retval
puts "Val: $retVal"

I get this output:
Val: ls: $CVSROOT: No such file or directory.


Any tips??

Thanks!

 
Well the problem with using '$env(CVSROOT)' is the fact that the user will be supplying the envrionment variables from the GUI. So i can't expect the user to type in the '$env()' around any env the want to use. Lets say i want to make it simpler for the user so they can just pass in '$CVSROOT' just like they would in a shell.
 
OK. Let's say the user types in "cvsroot" in an entry associated with the textvariable, edir (just like "set edir cvsroot").

set edir [string toupper $edir]

Then, a la marsd;

exec {ls $env($edir)}

Bob Rashkin
rrashkin@csc.com
 
The problem with that last post is that i can't count on the user to type in the env variables in a fixed pattern. He/she could type something like these below:

someprog -file $PATH/abc/def -file2 $PATH2/ghi
someprog $ABC
someprog $ABC/file1


so the problem is that i would have to do extensive parsing to figure out where the 'env variable' is in the string and then change it to '$env(VAR)'.

hope this explains the problem.
 
As ever, this would be a lot easier without users.

I suppose, while very inelegant, you could catch any error and just try again with a different pattern?

Bob Rashkin
rrashkin@csc.com
 
I am going to look into the 'regsub' command to do the parsing for me.

I will basically instruct it to look for '$...' and replace that with '$env(...)'

Any tips from the pattern matching gurus out there? =)
 
Here is the code i ended up using:

puts [regsub -all {\$[A-Z0-9]+} $userInput {$env(&)] junk]
puts "First round: $junk"
puts "Found: [regsub -all {\(\$} $junk {(} junk2]"

puts "Final result: $junk2"


Now if the user puts this in the text box:
$AVC/$PATH/file$AVC/ $VAR2

The result is:

First round: $env($AVC)/$env($PATH)/file$env($AVC)/ $env($VAR2)
Found: 4
Final Result: $env(AVC)/$env(PATH)/file$env(AVC)/ $env(VAR2)

PROBLEM SOLVED!!! (i think!.. i hope!!)

yay

thanks for all your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top