2012-07-10 46 views
1

我有我的tcsh啓動腳本如下代碼:在tcsh中,如何獲得反引號中命令的退出狀態?

set _color_count = `sh -c "tput -T${TERM}-256color colors 2>/dev/null"` 
if ($? == 0) then # check if it was a valid terminal type 
    if ($_color_count != 256) then # sanity-check 
     echo "Warning: Color count '$_color_count' for '${TERM}-256color' is not 256" 
    endif 
    setenv TERM "${TERM}-256color" 
endif 

我的問題是退出狀態($?)是總是爲零,即使tput命令返回非零退出狀態,由於到無效的終端類型。如果我不捕獲命令的輸出,檢查退出狀態正常工作:

sh -c "tput -T${TERM}-256color colors 2>/dev/null" 

如何確定tput命令是否返回非零退出狀態,因爲它是在反引號?

回答

3

事實證明,這是在tcsh版本6.17.05中引入的行爲更改(請參閱original bug report)。它看起來會從tcsh版本6.18.00(參見regression bug report)中恢復,但它顯然已經成爲狂野。

對於受影響的版本,不過,你可以在反引號運行此命令之前設置變量$ anyerror:

set anyerror 
set _color_count = `sh -c "tput -T${TERM}-256color colors 2>/dev/null"` 

根據特殊shell變量我tcsh手冊頁狀態

狀態最後一個命令返回的狀態,除非設置了變量anyerror,並且管道或反引用擴展中的任何錯誤都會被傳播(這是默認的csh行爲)。

相關問題