2014-10-29 54 views
0

我在.zshrc文件下面的函數(在OS X上,使用的coreutils日期執行):

# Display timestamped debugging messages when ZSHDBG is enabled. 
function zshdbg() { 
    if [[ $ZSHDBG -eq 1 ]]; then 
    purple="\x1b[35;01m" 
    reset="\x1b[39;49;00m" 
    now=$(date '+%F %T') 
    nanoseconds=$(date +%N) 

    # For some reason on OSX, %N is sometimes returned as "N" rather than the 
    # current nanosecond timestamp. This is a nasty hack for providing a 
    # default value in those unfortunate cases. 
    # if [[ "$nanoseconds" -eq 'N' ]]; then 
    # nanoseconds='000000000' 
    # fi 

    echo "$purple$now [$nanoseconds] $1$reset" 
    fi 
} 

出於某種原因,在納秒有時返回爲 「N」:

2014-10-28 21:45:42 [N] Antigen installation detected. 
2014-10-28 21:45:42 [N] Last update on . Current date 2014-10-28 21:45:42. It's been days since the last update. 
2014-10-28 21:45:42 [N] Loading antigen. 
2014-10-28 21:45:42 [N] Setting oh-my-zsh as the default antigen plugin repository. 
2014-10-28 21:45:42 [N] Enabling OS X specific plugins. 
2014-10-28 21:45:42 [N] Loading plugin zsh-users/zsh-syntax-highlighting. 
2014-10-28 21:45:42 [N] Loading theme jonathan. 
2014-10-28 21:45:42 [N] Applying the antigen configuration. 
2014-10-28 21:45:42 [N] Setting default exports. 
2014-10-28 21:45:43 [N] OS X detected, executing OS specific configuration. 
2014-10-28 21:45:43 [N] Homebrew detected. Configuring paths. 
2014-10-28 21:45:43 [084632000] Finished loading .zshrc. 

這是使用的coreutils date命令:

which date 
/usr/local/opt/coreutils/libexec/gnubin/date 

但是,如果我顯式調用/ bin中/日,我仍然得到類似的行爲:

2014-10-28 21:50:47 [N] Antigen installation detected. 
2014-10-28 21:50:47 [N] Last update on . Current date 2014-10-28 21:50:47. It's been days since the last update. 
2014-10-28 21:50:47 [N] Loading antigen. 
2014-10-28 21:50:47 [N] Setting oh-my-zsh as the default antigen plugin repository. 
2014-10-28 21:50:47 [N] Enabling OS X specific plugins. 
2014-10-28 21:50:47 [N] Loading plugin zsh-users/zsh-syntax-highlighting. 
2014-10-28 21:50:47 [N] Loading theme jonathan. 
2014-10-28 21:50:47 [N] Applying the antigen configuration. 
2014-10-28 21:50:47 [N] Setting default exports. 
2014-10-28 21:50:47 [N] OS X detected, executing OS specific configuration. 
2014-10-28 21:50:47 [N] Homebrew detected. Configuring paths. 
2014-10-28 21:50:47 [N] Finished loading .zshrc. 

我一直在挖掘date,zsh和homebrew的coreutils的文檔,但沒有找到任何相關的東西。任何想法,爲什麼這可能會發生?

回答

0

周圍一點點瞎搞後,我發現了以下工作:

function zshdbg() { 
    if [[ $ZSHDBG -eq 1 ]]; then 
    purple="\x1b[35;01m" 
    reset="\x1b[39;49;00m" 
    now=$(date '+%F %T') 
    nanoseconds=$(date +%N) 

    echo $purple$now" ["$nanoseconds"] "$1$reset 
    fi 
} 

了它的輸出:

2014-10-28 22:27:32 [647272000] Antigen installation detected. 
2014-10-28 22:27:32 [663708000] Last update on 1414545789. Current date 1414556852. It's been days since the last update. 
2014-10-28 22:27:32 [669752000] Loading antigen. 
2014-10-28 22:27:32 [681586000] Setting oh-my-zsh as the default antigen plugin repository. 
2014-10-28 22:27:32 [807303000] Enabling OS X specific plugins. 
2014-10-28 22:27:32 [944470000] Loading plugin zsh-users/zsh-syntax-highlighting. 
2014-10-28 22:27:33 [034537000] Loading theme jonathan. 
2014-10-28 22:27:33 [125400000] Applying the antigen configuration. 
2014-10-28 22:27:33 [171657000] Setting default exports. 
2014-10-28 22:27:33 [303480000] Finished loading .zshrc. 

隨着納秒被在所有情況下正確生成。不確定到底發生了什麼事。它可能與括號在$ nanoseconds變量替換周圍格式化的方式有關。

1

隨OS X提供的date的實現不支持+N,因此/bin/date +N只是打印「N」。我懷疑你的PATH不一致,具體說/usr/local/opt/coreutils/libexec/gnubin不是(總是)在/bin之前列出的。試試這個:

# Display timestamped debugging messages when ZSHDBG is enabled. 
function zshdbg() { 
    if [[ $ZSHDBG -eq 1 ]]; then 
    purple="\x1b[35;01m" 
    reset="\x1b[39;49;00m" 
    now=$(date '+%F %T') 
    nanoseconds=$(/usr/local/opt/coreutils/libexec/gnubin/date +%N) 

    echo "$purple$now [$nanoseconds] $1$reset" 
    fi 
} 

如果修復它,你應該嘗試追查做什麼用PATH搞亂,因爲這可能不是唯一的影響...

+0

我認爲這是路徑和格式的結合 - 兩個問題我實際上在同一時間修復,但沒有意識到另一個也是一個罪魁禍首。當我進行原始測試時,我正在檢查我們是否在OSX上,然後在* oh-my-zsh被抗原加載後預先加入coreutils *,但是在我的zsh配置之前調用該函數之前, coreutils/homebrew路徑是前綴。這段代碼被移動到我的.zshenv文件的開頭,這樣子路徑是正確的。似乎現在工作正常。 – nfarrar 2014-10-29 14:36:16

+0

@nfarrar實際上,我會將格式恢復到以前的樣子 - 除極少數例外情況外,您應該在雙引號之間保留可變引用以防止意外的分詞和通配符擴展。在這種情況下,它可能是安全的,但看看[這個最近的問題和它的答案](http://stackoverflow.com/questions/26616849/how-to-set-shell-variable-to-result-of-evaluation -in-os-x)查看未引用變量引用的意外結果示例。 – 2014-10-29 15:24:16

0

由於版本4.3.12(2011年) ,zshzsh/datetime模塊中有epochtime陣列和EPOCHREALTIME變量,因此您不需要調用外部date命令(這將需要幾千納秒運行)。

$ zmodload zsh/datetime 
$ strftime "%F %T [$epochtime[2]]" $epochtime[1] 
2014-10-31 16:17:13 [698637482] 
相關問題