Tcl/Expect中利用exec調用稍微複雜的shell命令時,經常會遇到一些小問題,常見的就是pipeline(|)和awk。
Tcl/Expect調用多個shell命令並使用|將其串接在一起時,需要注意的是必須在"|"前後加上空格" ",否則tcl/Expect會報奇怪的錯。
Tcl/Expect調用awk命令時,需要把awk的' '中的命令改為用" ",並把$1, $2之類的變數改為/$1, /$2。
下面的ksh命令判斷test_process是否在運行:
ps -fu bonny|grep -v ps|grep MMCAP|grep -v grep|awk '{print $2}'
改為Tcl/Expect後,為:
exec ps -fu $myname | grep -v ps | grep COOLrunMMCAP | grep -v grep | awk "{print /$2}"
另外, 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的一些常見問題。