Tcl exec on grep returns "grep: nothing to repeat",而正常 LINUX 给出正确答案

Tcl exec on grep returns "grep: nothing to repeat", whereas normal LINUX gives right answer

下面是我 运行 使用 cshell 命令时发生的情况:

find /projects/corpeng/members/ldagan/adm/utils/Synopsys/CCK/shared_tcl_code -name "*.tcl" | grep -v regression | xargs grep -Plc "^[ ]*namespace[ ]+eval[ ]+DlgViolationKeepProcs "
> /projects/corpeng/members/ldagan/adm/utils/Synopsys/CCK/shared_tcl_code/DlgViolationKeepProcs.tcl

到目前为止,一切都很好。但是,看看当我尝试在 TCL 8.4 中执行相同操作时会发生什么:

exec find /projects/corpeng/members/ldagan/adm/utils/Synopsys/CCK/shared_tcl_code -name "*.tcl" | grep -v regression | xargs grep -Plc "^[ ]*namespace[ ]+eval[ ]+DlgViolationKeepProcs "
> grep: nothing to repeat

为什么?有什么想法吗?
谢谢。

问题是双引号中的 [] 被处理为 Tcl 命令替换。特别是,[ ] 被替换为空字符串,即被删除!最简单的解决方法是将 grep 术语放在 { 大括号 } 中以禁止替换,尽管在 [ 前面放置反斜杠 (\) 也可以。

让我们通过使用一些策略变量让您的代码更易于阅读:

# Where to search from; often a nice thing to separate out
set basedir /projects/corpeng/members/ldagan/adm/utils/Synopsys/CCK/shared_tcl_code

# What to look for; note the braces!
set term {^[ ]*namespace[ ]+eval[ ]+DlgViolationKeepProcs }

exec find $basedir -name "*.tcl" | grep -v "regression" | xargs grep -Plc $term

如果你说:

set term "^\[ ]*namespace\[ ]+eval\[ ]+DlgViolationKeepProcs "

但经验告诉我尽量避免不必要地使用反斜杠;从长远来看,他们会更加困惑。