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

來源:互聯網
上載者:User

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的一些常見問題。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.