Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TCL quoting question

81 views
Skip to first unread message

Kenny McCormack

unread,
Mar 4, 2018, 8:09:58 AM3/4/18
to
My background/position: I do a lot of Expect programming, but try to do as
little TCL as I can get away with.

My problem: In Expect, I want to do something like:

system someShellCommand $somevariable "parameter that might have spaces in it"

Now, normally, when I do a shell (system) command, I wrap it in {} so that
TCL leaves it alone. But, here, I need to pass the value of the TCL
variable ("somevariable"), so I can't do that. but if I leave it as above,
then TCL interprets the quotes and I've gotten errors. Specifically, as
shown below (this example is a little convoluted and complex, but it shows
the point):

$ expect -c 'set foo hello;system echo $foo\;set -- "goodbye to you"\;echo \$#,\$1'
extra characters after close-quote
while executing
"system echo $foo\;set -- "goodbye to you"\"

I don't get why this error happens. Note that above does run without error
if I leave off the last part (\;echo \$#,\$1). But I need that part to
verify that the part before it actually did what it was supposed to.

What is the proper way to quote the above command so that it works as
expected?

--
Rich people pay Fox people to convince middle class people to blame poor people.

(John Fugelsang)

Rich

unread,
Mar 5, 2018, 1:54:41 PM3/5/18
to
Kenny McCormack <gaz...@shell.xmission.com> wrote:
> $ expect -c 'set foo hello;system echo $foo\;set -- "goodbye to you"\;echo \$#,\$1'
> extra characters after close-quote
> while executing
> "system echo $foo\;set -- "goodbye to you"\"
>
> ..
>
> What is the proper way to quote the above command so that it works as
> expected?

If you want the double quotes to go to the shell, then you have to tell
Tcl to ignore them:

expect -c 'set foo hello; system echo $foo\;set -- \"goodbye to you\"\;echo \$#,\$1'

$ expect -c 'set foo hello; system echo $foo\;set -- \"goodbye to you\"\;echo \$#,\$1'
hello
1,goodbye to you
$

That looks reasonable to me as output from the input, but I don't know
what you expected it to return.

wil...@gmail.com

unread,
Mar 5, 2018, 2:00:58 PM3/5/18
to
Separate the call to the set command and whatever comes after it with a space char:

set -- "goodbye to you" \;

Result:

expect -c 'set foo hello; system echo $foo\; set -- "goodbye to you" \;echo \$#,\$1'
hello
3,goodbye

Matthew Hiles

unread,
Mar 5, 2018, 3:44:59 PM3/5/18
to
Seems like a good opportunity to use subst.

> expect -c 'set foo hello;system [subst {echo $foo\;set -- "goodby to you"\;echo \$#,\$1}]'
hello
1,goodby to you

wil...@gmail.com

unread,
Mar 5, 2018, 5:11:59 PM3/5/18
to
Yeah, subst seems like the best solution but his problem is shell/interpreter quoting.

Let me show both tcl and shell solutions.

Tcl:
If we put all this into a tcl file and source it from within expect, then this is how you get the tcl quoting and escaping of special characters correct. Outside of tcl on the command line you have to additionally account for the shell/interpreter.

% expect
expect1.1> source ./j.tcl
hello
1,goodbye to you
expect1.2> exit

% cat ./j.tcl
set foo hello
system "echo $foo\; set -- \"goodbye to you\"\; echo \$#,\$1"


Here is the shell/cmd line fix:

Original:
% expect -c 'set foo hello;system echo $foo\;set -- "goodbye to you"\;echo \$#,\$1'
(failed, see original error msg)

Fixed:
% expect -c 'set foo hello;system "echo $foo\;set -- \"goodbye to you\"\; echo \$#,\$1"'
hello
1,goodbye to you

(added double quotes around the cmds passed to system call)
0 new messages