在两个字符串上使用tcl exec diff模拟diff输出

时间:2016-03-07 07:39:41

标签: string tcl exec diff tclsh

我试图在tclsh中使用tcl exec diff在两个字符串上生成相同的diff命令输出。

请您告诉我如何使用tclsh exec diff

解决此示例

使用Bash

$ diff <(echo "Testword") <(echo "Testword2")
1c1
< Testword
---
> Testword2

TCL失败

set str1 "Testword"
set str2 "Testword2"

首次尝试

% exec diff <(echo "$string1") <(echo "$string2")
extra characters after close-quote

第二次尝试

% exec diff <(echo \"$string1\") <(echo \"$string2\")
couldn't read file "(echo": no such file or directory

第三次尝试

% exec diff \<(echo \"$string1\") \<(echo \"$string2\")
couldn't read file "(echo": no such file or directory

第四次尝试

% set command [concat /usr/bin/diff <(echo \\"$string1\\") <(echo \\"$string2\\")]
/usr/bin/diff <(echo \"Malli\") <(echo \"Malli1\")
% exec $command
couldn't execute "/usr/bin/diff <(echo \"Malli\") <(echo \"Malli1\")": no such file or directory.

1 个答案:

答案 0 :(得分:3)

这有点棘手,因为你依赖于bash功能 - 将<(some string)转换为看起来像文件的东西 - 但是Tcl的exec不会调用bash或者自己进行这种转换。您可以通过从Tcl显式调用bash来使其工作:

% exec bash -c "diff <(echo '$string1') <(echo '$string2') || /bin/true"
1c1
< Testword
---
> Testword2
% 

请注意

  • 这里引用的嵌套是有效的,因为单引号'对于bash是特殊的,但对于Tcl
  • 是特殊的
  • 添加|| / bin / true是一个强制零(成功)退出代码的hack - 如果没有这个,你会收到一条错误消息“子进程异常退出”,因为diff在其输入不同时返回非零退出状态。 `