simulate diff output using tcl exec diff on two strings

让人想犯罪 __ 提交于 2019-12-08 02:46:12

问题


I am trying to produce same output of diff command in tclsh using tcl exec diff on two strings.

Could you please let me know how to solve this example using tclsh exec diff

Working with Bash

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

Failed on TCL

set str1 "Testword"
set str2 "Testword2"

First attempt

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

Second attempt

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

Third attempt

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

Fourth attempt

% 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:


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
% 

Note that

  • The nested quoted here works because single-quote ' is special to bash but not to Tcl
  • Adding || /bin/true is a hack to force a zero (success) exit code - without this you get an error message "child process exited abnormally" because diff returns a non-zero exit status when its inputs differ. `


来源:https://stackoverflow.com/questions/35838497/simulate-diff-output-using-tcl-exec-diff-on-two-strings

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