Tcl/Expect中利用exec调用稍微复杂的shell命令时,经常会遇到一些小问题,常见的就是pipeline(|)和awk。
Tcl/Expect调用多个shell命令并使用|将其串接在一起时,需要注意的是必须在"|"前后加上空格" ",否则tcl/Expect会报奇怪的错。
Tcl/Expect调用awk命令时,需要把awk的' '中的命令改为用" ",并把$1, $2之类的变量改为/$1, /$2。
下面的ksh命令判断test_process是否在运行:
改为Tcl/Expect后,为:
另外, http://www.linuxquestions.org/questions/linux-software-2/ksh-tcl-173092/ 列出了一些ksh命令转换为tcl语句时经常遇到的问题(本人未曾验证过,请谨慎使用)。
Here's some conversion rules that I have proven, if anyone has any more info ( in any fashion ) , please advise.
-----------------------------------------------------------------
Rule: Remove "
-----------------------------------------------------------------
ksh: cat *.passwd 2>/dev/null | cut -d":" -f1 | sort | uniq
tcl: exec cat *.passwd 2>/dev/null | cut -d: -f1 | sort | uniq
-----------------------------------------------------------------
-----------------------------------------------------------------
Rule: Replace '(..$1..)' with "{../$1..}"
-----------------------------------------------------------------
ksh: ps -eaf | awk '(print $1}' | grep $1 2>&1
tcl: exec ps -eaf | awk "{print /$1}" | grep $user_login_to_test
-----------------------------------------------------------------
-----------------------------------------------------------------
Rule: Replace "..." with {...}
-----------------------------------------------------------------
ksh: grep "^$(user_login:" /etc/passwd /etc/shadow 2>&1
tcl: exec grep {^$user_login:} /etc/passwd /etc/shadow
-----------------------------------------------------------------
-----------------------------------------------------------------
Rule: Replace '...' with "..." or {...}
-----------------------------------------------------------------
ksh: grep 'Version' user.tcl
tcl: exec grep {Version} user.tcl
tcl: exec grep "Version" user.tcl
-----------------------------------------------------------------
还有就是http://wiki.tcl.tk/1039列出了使用exec的一些常见问题。

本文介绍了在Tcl/Expect中使用exec调用shell命令时,特别是涉及管道"|"和awk时需要注意的事项。强调了在使用管道时需在"|”前后加空格,awk命令中的'$1'等需改为'/$1'。同时提供了几个ksh到Tcl的转换规则示例,并提到了相关资源以获取更多信息。
68

被折叠的 条评论
为什么被折叠?



