本文介绍了在两个字符串上使用tcl exec diff模拟diff输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在两个字符串上使用tcl exec diff在tclsh中产生相同的diff命令输出.
I am trying to produce same output of diff command in tclsh using tcl exec diff on two strings.
请让我知道如何使用tclsh exec diff解决此示例
Could you please let me know how to solve this example using 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.
推荐答案
这有点棘手,因为您依赖bash功能-将<(some string)
转换为看起来像文件的内容-但Tcl的exec不会调用bash或单独执行此转换.您可以通过从Tcl显式调用bash使其工作:
This is a little tricky because you are relying on a bash feature - converting <(some string)
to something that looks like a file - but Tcl's exec does not invoke bash or do this conversion by itself. You can make it work by explicitly calling bash from Tcl:
% exec bash -c "diff <(echo '$string1') <(echo '$string2') || /bin/true"
1c1
< Testword
---
> Testword2
%
请注意
- 这里引用的嵌套引用有效,因为单引号'对bash而言是特殊的,但对Tcl而言则不是
- 添加||/bin/true是一种强制执行零(成功)退出代码的技巧-如果没有此操作,您将收到一条错误消息子进程异常退出",因为diff在其输入不同时会返回非零退出状态. `
这篇关于在两个字符串上使用tcl exec diff模拟diff输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!