2017-06-20 79 views
0

我的.zshenv產生的輸出僅在我的外殼交互時纔有用。 在其他情況下,當shell不交互時,這個輸出(即當我運行腳本時)必須被隱藏。爲非交互式外殼禁用.zshenv輸出

如何爲非交互式shell壓縮.zshenv輸出?

P.S.我在下面添加了我當前的解決方案,但對我來說似乎很難受。

+0

將代碼放在'.zshrc',而不是'.zshenv'如果它不從做任何事情擱置產生輸出。 – chepner

+0

@chepner感謝您的建議,但實際上我將代碼從'.zshrc'移至'.zshenv',因爲它爲交互式和非交互式shell設置了環境。 – mixel

回答

0

My other answer在某些情況下無法正常工作。

我搬到.zshenv內容到功能run並添加條件重定向此功能輸出:

function run() { 
    // .zshenv content 
} 

OUTPUT=1 

if [[ ! -o interactive ]]; then 
    OUTPUT=3 
    eval "exec $OUTPUT<>/dev/null" 
fi 

run >& $OUTPUT 

if [[ ! -o interactive ]]; then 
    eval "exec $OUTPUT>&-" 
fi 
1

我說:

if [[ ! -o interactive ]]; then 
    exec 1>&- 
    exec 1<>/dev/null 
fi 

.zshenv文件的頂部,關閉原來的stdout 1文件描述符並分配/dev/null它。

,並在底部,我添加:

if [[ ! -o interactive ]]; then 
    exec 1>&0 
fi 

恢復它(見Reopen STDOUT and STDERR after closing them?)。