Tcl/Expect中利用exec调用管道"|"和awk的注意事项

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}"

另外, 列出了一些ksh命令转换为tcl语句时经常遇到的问题(本人未曾验证过,请谨慎使用)。

Heres 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 -----------------------------------------------------------------

还有就是列出了使用exec的一些常见问题。

经验分享 程序员 微信小程序 职场和发展