TCL problem. Can someone help?

David Rysdam david at rysdam.org
Thu Nov 4 11:41:31 EDT 2010


An agent or agents purporting to be Steven W. Orr said:
> I have a stupid question in tcl that I'm just not getting. I'm hoping to get 
> lucky here.
> 
> I have a script in tcl/expect that spawns su and needs to pass its arguments 
> to su.
> 
> argv in tcl has the command line args. I lop off the first couple of args that 
> I need in my script via:
> 
> set user [lindex $argv 0]
> set passwd [lindex $argv 1]
> 
> Then I want to pass the *rest* of the args to su. What I have is this:
> 
> spawn su - $user -c "[lrange $argv 2 end]"
> 
> If I call me script
> 
> sss me secret 'pwd; ls'
> 
> Then what happens is this:
> 
> spawn su - swagent -c {pwd; ls;}
> Password:
> -bash: -c: line 0: syntax error near unexpected token `}'
> -bash: -c: line 0: `{pwd; ls;}'
> 
> I vershtumped about where the braces are coming from. found out that if I pass 
> a single command without any semicolon , it works ok.

The braces are there because the result of the lrange is a list.  The
way we handle this in our code (which may not be The Right Way) is by
doing something this:

set args [lrange $argv 2 end]
eval {spawn su - $user -c} "$args"

(I don't know if you need the quotes for either spawn or su, so those
might be extraneous or need some special quoting or something.)

Newer versions for Tcl (and therefore Expect?) also have an expand
operator that probably does this better, but I don't know if you are
using that.


More information about the gnhlug-discuss mailing list