如何在 tcl 脚本中针对 glob 进行提取?

How to extract against glob in tcl script?

我将一组文件存储到一个变量中并传递该变量以获取变量 A 各自的重复值和唯一值。

set A "232 234 234 234"
set a 1
set files_name "/usr/test/a_232.txt /usr/test/a1_234.txt /usr/test/a2_234.txt /usr/test/a3_234.txt"

foreach j [split $A " "] {
    incr count($j)
}
foreach key [array names count] {
    if {$count($key) == 1} {
        set file_name1 [glob -type f $file_name {$key} ]
    } else {
        set file_name2 [glob -type f $file_name {$key} ]
    }
}

每当我执行上面的代码时,我都会遇到以下错误

no files matched glob patterns "/usr/test/a_232.txt /usr/test/a1_234.txt /usr/test/a2_234.txt /usr/test/a3_234.txt"
    while executing
"glob -type f $file_list {$key} "

结果应该是这样的

file_name1 : /usr/test/a_232.txt
file_name2 :/usr/test/a1_234.txt /usr/test/a2_234.txt /usr/test/a3_234.txt

我想你想做的是这样的:

set A "232 234 234 234"
set files_name "/usr/test/a_232.txt /usr/test/a1_234.txt /usr/test/a2_234.txt /usr/test/a3_234.txt"

unset -nocomplain count
foreach j [split $A] {
    incr count($j)
}
foreach key [array names count] {
    if {$count($key) == 1} {
        set file_name1 [lsearch -regexp -inline $files_name $key]
    } else {
        set file_name2 [lsearch -regexp -inline -all $files_name $key]
    }
}

注意:file_name2 的名称与文件名列表中键的匹配项一样多,可能多于或少于 A 中该键的出现次数。

glob 命令在文件系统中搜索文件名,而不是在文件列表中。

文档:array, foreach, if, incr, lsearch, split, unset