vkolodrevskiy
vkolodrevskiy

Reputation: 599

TCL exec with special characters in list

There is tcl procedure which executes command stored in tcl list. For example:

catch { exec $list}

List looks something like:

--option1 op1 --option2 op2 --option3 op3 ...

One of options is regexp that looks like:

(.*[/\])?(sh|bash)(\.exe)?

After substitution by exec option looks like:

{(.*[/\])?(sh|bash)(\.exe)?}

But what I need is:

"(.*[/\])?(sh|bash)(\.exe)?"

What can I do in such situation?

Upvotes: 2

Views: 1277

Answers (2)

Mike White
Mike White

Reputation: 1

  1. exec is going to execute commnad as subprocess

  2. See semples :

    works :

    exec ps aux | grep tclsh

    not working :

    exec "ps aux | grep tclsh"

    exec [list ps aux | grep tclsh]

    but works fine :

    eval exec "ps aux | grep tclsh"

    eval exec [list ps aux | grep tclsh]

So we have got evaluating before executing - creation command(exec ps aux | grep tclsh) and invoking it. So eval doesn't care about type.

  1. For current situation with options: exec someExecutable --option1 op1 --option2 op2 --option3 (.*[/\])?(sh|bash)(\.exe)?

I would recoment such solution:

set op3 {"(.*[/\])?(sh|bash)(\.exe)?"}
lappend cmd  [someExecutable --option1 op1 --option2 op2 --option3 $op3]
set cmd [join $cmd]
eval exec $cmd

where [join $cmd] creates string from list - and there is not any { }

Upvotes: 0

Brad Lanam
Brad Lanam

Reputation: 5733

When a list is converted to a string, it is converted to a canonical form that will convert back to the same list.

What you are seeing are the quoting characters that are used to ensure that the canonical form converts back correctly.

So the value is correct.

exec $list only passes a single argument to exec. exec takes a series of words as arguments, not a list which contains words.

The exec command should be:

catch { exec {*}$list }

The {*} syntax converts the list into its component words.

In older versions of tcl, the eval statement must be used:

catch { eval exec $list }

References: exec, eval, {*} (section 5 of Tcl)

Upvotes: 4

Related Questions