TCL exec with special characters in list

拟墨画扇 提交于 2019-12-08 02:01:41

问题


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?


回答1:


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)




回答2:


  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 { }



来源:https://stackoverflow.com/questions/39455292/tcl-exec-with-special-characters-in-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!